title: HTML FORM + PHP: form action to remain on same page as login
body: <p>at the moment my form links to a new page with all of my php code on it. I would like for all of the code to be executed on the same page as it does in this tutorial: <a href="http://www.w3schools.com/php/php_form_url_email.asp" rel="nofollow">http://www.w3schools.com/php/php_form_url_email.asp</a></p>
  
  <p>I'm having some difficulty understanding exactly how this tutorial manages it. I was thinking maybe it would be possible to store my add.php code in a function and then call it with form action. Is this possible? If not what would be the best way to go about this?</p>
  
  <p>here is my form code: </p>
  
  <pre><code>&lt;form action="add.php" method="post"&gt;
  
      &lt;p&gt;
             Username: &lt;input type="text" name="Username"&gt;&lt;br&gt;
             Email: &lt;input type="text" name="Email"&gt;&lt;br&gt;
             Password: &lt;input type="password" name="Password"&gt;&lt;br&gt;
             Confirm Password: &lt;input type="password" name="ConfirmPass"&gt;
      &lt;/p&gt;
  
      &lt;p&gt;
             &lt;input type="submit"&gt;
      &lt;/p&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>and here is my add.php page:</p>
  
  <pre><code>&lt;?php
  
  $Username = $_POST['Username'];
  $Password = $_POST['Password'];
  $Email = $_POST['Email'];
  $ConfirmPass = $_POST['ConfirmPass'];
  $safeUsername = SQLite3::escapeString($Username);
  $safePassword = SQLite3::escapeString($Password);
  $safeEmail = SQLite3::escapeString($Email);
  $safeConfirmPass = SQLite3::escapeString($ConfirmPass);
  $hostName = explode('@', $Email);
  
  
  $database = new PDO('sqlite:maindb.db');
  
  $sql = "SELECT * FROM users WHERE Username = ?";
  $result = $database-&gt;prepare($sql);
  $result-&gt;bindParam(1, $safeUsername);
  $result-&gt;execute();
  
  
  
      if($result-&gt;fetch()) {  
              echo "Username " . $safeUsername . " already exists";
      }
      else { 
                  if (filter_var($safeEmail, FILTER_VALIDATE_EMAIL) &amp;&amp; checkdnsrr($hostName[1]) &amp;&amp; !empty($safeEmail)) {
                      if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$safeEmail)) {
                          if (!empty($safeUsername) &amp;&amp; (preg_match("/^[a-zA-Z ]*$/",$safeUsername)) &amp;&amp; !empty($safeUsername)) {
  
                              if ($safePassword == $safeConfirmPass &amp;&amp; !empty($safePassword)) {
                                  echo $Username . " was successfully added to the database.";
                                  $stm = "INSERT INTO users(Username, Password, Email) VALUES(?,?,?)";
                                  $stmt = $database-&gt;prepare($stm);
                                  $stmt-&gt;bindParam(1, $safeUsername);
                                  $stmt-&gt;bindParam(2, $safePassword);
                                  $stmt-&gt;bindParam(3, $safeEmail);
                                  $stmt-&gt;execute();
  
                                  $database = null;
                              }
                              else {echo "Passwords do not match"; }
  
                          }
                          else {echo "Invalid Username.";}
                      }
                      else {echo "Invalid e-mail address.";}
                  }
                  else {echo "Invalid e-mail address.";}
          }
  
  
  
  ?&gt;
  </code></pre>
  
  <p>Thanks for the help! I appreciate it.</p>