title: Using PHP POST method to post to the same page
body: <p>Tried figuring this out on my own using previous threads, but other peoples' examples didn't quite help me figure out what I need. The stand-alone files themselves work, data gets inserted into the DB just fine... but I'm trying to adapt these documents a bit to work as an all-in-one, single PHP document.</p>
  
  <p>I figure I'll just throw out what I'm trying to do and the code I have so far, and maybe someone will understand what I'm trying to accomplish and be able to explain how to do it.</p>
  
  <p>I basically have an PHP document with form (form.php) that uses POST to send the form data to a second PHP document, which in turn sends the form data to a MySQL database. I want the user to be able to post the data using the same document and have the data fields reset so that the user and submit enter more information through the form. I would like the make the user aware of errors due to incomplete fields, and I would like the user to be made aware of successful entries (which I will do using the echo function).</p>
  
  <p>form.php is as follows:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;link rel="stylesheet" type="text/css" href="form.css"&gt;
      &lt;title&gt;MySQL Database Creation System&lt;/title&gt;
      &lt;script src="jquery-1.11.0.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class="wrapper"&gt;
          &lt;img src="eris.png" align="middle"&gt;
          &lt;form action="insert.php" method="post"&gt;
          Firstname: &lt;input type="text" size="16" name="firstname"&gt;
          Lastname: &lt;input type="text" size="16" name="lastname"&gt;
          Age: &lt;input type="text" size="1" maxlength="3" name="age"&gt;
          &lt;input type="submit" class="button"&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>insert.php is as follows:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;link rel="stylesheet" type="text/css" href="insert.css"&gt;
      &lt;title&gt;MySQL Database Creation System&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class="wrapper"&gt;
      &lt;img src='eris.png' align='middle'&gt;
          &lt;?php
          if ( (trim($_POST['firstname']) == "") || (trim($_POST['lastname']) == "") || (trim($_POST['age']) == "") )
          {
              echo "&lt;p&gt;ERROR: All fields must be completed&lt;/p&gt;";
              echo "&lt;p&gt;&lt;a href='form.php'&gt;Return to Form&lt;/a&gt;&lt;/p&gt;";
          }
          else
          {
              $con=mysqli_connect("localhost","test","test","my_db");
              // Check connection
              if (mysqli_connect_errno())
              {
                echo "&lt;br&gt;Failed to connect to MySQL: " . mysqli_connect_error();
              }
  
              // escape variables for security
              $firstname = mysqli_real_escape_string($_POST['firstname']);
              $lastname = mysqli_real_escape_string($_POST['lastname']);
              $age = mysqli_real_escape_string($_POST['age']);
  
              $sql="INSERT INTO Persons (FirstName, LastName, Age) 
              VALUES ('$firstname', '$lastname', '$age')";
  
              if (!mysqli_query($con,$sql))
              {
                die('&lt;br&gt;Error: ' . mysqli_error($con));
              }
  
              mysqli_close($con);
  
              echo "&lt;p&gt;New item added.&lt;/p&gt;";
              echo "&lt;p&gt;&lt;a href='form.php'&gt;Return to Form&lt;/a&gt;&lt;/p&gt;";
          }
          ?&gt;
      &lt;/div&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>Thanks in advance for any assistance you can give.</p>