PHP form - on submit stay on same page

后端 未结 9 801
醉梦人生
醉梦人生 2020-11-29 01:49

I have a PHP form that is located on file contact.html.

The form is processed from file processForm.php.

When a user fills out the

相关标签:
9条回答
  • 2020-11-29 01:56

    You have to use code similar to this:

    echo "<div id='divwithform'>";
    
    if(isset($_POST['submit']))  // if form was submitted (if you came here with form data)
    {
        echo "Success";
    }
    else                // if form was not submitted (if you came here without form data)
    {
        echo "<form> ... </form>";
    } 
    
    echo "</div>";
    

    Code with if like this is typical for many pages, however this is very simplified.

    Normally, you have to validate some data in first "if" (check if form fields were not empty etc).

    Please visit www.thenewboston.org or phpacademy.org. There are very good PHP video tutorials, including forms.

    0 讨论(0)
  • 2020-11-29 02:00

    You can see the following example for the Form action on the same page

    <form action="" method="post">
    <table border="1px">
        <tr><td>Name: <input type="text" name="user_name" ></td></tr>
        <tr><td align="right"> <input type="submit" value="submit" name="btn"> 
    </td></tr>
    </table>
    </form>
    
    <?php
      if(isset($_POST['btn'])){
         $name=$_POST['user_name'];
         echo 'Welcome '. $name; 
       }
     ?>
    
    0 讨论(0)
  • 2020-11-29 02:04

    In order to stay on the same page on submit you can leave action empty (action="") into the form tag, or leave it out altogether.

    For the message, create a variable ($message = "Success! You entered: ".$input;") and then echo the variable at the place in the page where you want the message to appear with <?php echo $message; ?>.

    Like this:

    <?php
    $message = "";
    if(isset($_POST['SubmitButton'])){ //check if form was submitted
      $input = $_POST['inputText']; //get input text
      $message = "Success! You entered: ".$input;
    }    
    ?>
    
    <html>
    <body>    
    <form action="" method="post">
    <?php echo $message; ?>
      <input type="text" name="inputText"/>
      <input type="submit" name="SubmitButton"/>
    </form>    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-29 02:05

    There are two ways of doing it:

    1. Submit the form to the same page: Handle the submitted form using PHP script. (This can be done by setting the form action to the current page URL.)

      if(isset($_POST['submit'])) {
          // Enter the code you want to execute after the form has been submitted
          // Display Success or Failure message (if any)
        } else {
          // Display the Form and the Submit Button
      }
      
    2. Using AJAX Form Submission which is a little more difficult for a beginner than method #1.

    0 讨论(0)
  • 2020-11-29 02:11

    You can use the # action in a form action:

    <?php
        if(isset($_POST['SubmitButton'])){ // Check if form was submitted
    
            $input = $_POST['inputText']; // Get input text
            $message = "Success! You entered: " . $input;
        }
    ?>
    
    <html>
        <body>
            <form action="#" method="post">
                <?php echo $message; ?>
                <input type="text" name="inputText"/>
                <input type="submit" name="SubmitButton"/>
            </form>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-11-29 02:11

    What I do is I want the page to stay after submit when there are errors...So I want the page to be reloaded :

    ($_SERVER["PHP_SELF"])
    

    While I include the sript from a seperate file e.g

    include_once "test.php";
    

    I also read somewhere that

    if(isset($_POST['submit']))
    

    Is a beginners old fasion way of posting a form, and

    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    

    Should be used (Not my words, read it somewhere)

    0 讨论(0)
提交回复
热议问题