title: how to display errors on the same page (php form)
body: <p>Trying to display errors on the same page. How do I do that? As this stands right now, every time there are errors, it generates a blank page with all the errors listed. </p>
  
  <p>I don't want that. </p>
  
  <p>Here's a basic form that I ripped off from a tutorial somewhere <code>test.php</code>: </p>
  
  <pre><code> &lt;?php session_start(); ?&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
          &lt;form name="form1" method="post" action="test2.php"&gt;
          Name: &lt;br/&gt;
          &lt;input type="text" name="name" value="&lt;?php echo $_POST['name']; ?&gt;" size="50" /&gt;&lt;br/&gt;&lt;br/&gt;
          Email Address: &lt;br/&gt;
          &lt;input type="text" name="email" value="&lt;?php echo $_POST['email']; ?&gt;" size="50"/&gt; &lt;br/&gt;&lt;br/&gt;
          Home Page: &lt;br/&gt;
          &lt;input type="text" name="homepage" value="&lt;?php echo $_POST['homepage']; ?&gt;" size="50" /&gt; &lt;br/&gt;&lt;br/&gt;
          Message: &lt;br/&gt;
          &lt;textarea name="message" rows="5" cols="50"&gt;&lt;?php echo $_POST['message']; ?&gt;&lt;/textarea&gt;
          &lt;br/&gt;
          &lt;input type="submit" name="Submit" /&gt;
          &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here's the php code for it that was also ripped off from the tutorial, ie, <code>test2.php</code>: </p>
  
  <pre><code>&lt;?php
  
      session_start();
  
      if (isset($_POST['Submit'])) {
  
          if ($_POST['name'] != "") {
              $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
              if ($_POST['name'] == "") {
                  $errors .= 'Please enter a valid name.&lt;br/&gt;&lt;br/&gt;';
              }
          } else {
              $errors .= 'Please enter your name.&lt;br/&gt;';
          }
  
          if ($_POST['email'] != "") {
              $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
              if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                  $errors .= "$email is &lt;strong&gt;NOT&lt;/strong&gt; a valid email address.&lt;br/&gt;&lt;br/&gt;";
              }
          } else {
              $errors .= 'Please enter your email address.&lt;br/&gt;';
          }
  
          if ($_POST['homepage'] != "") {
              $homepage = filter_var($_POST['homepage'], FILTER_SANITIZE_URL);
              if (!filter_var($homepage, FILTER_VALIDATE_URL)) {
                  $errors .= "$homepage is &lt;strong&gt;NOT&lt;/strong&gt; a valid URL.&lt;br/&gt;&lt;br/&gt;";
              }
          } else {
              $errors .= 'Please enter your home page.&lt;br/&gt;';
          }
  
          if ($_POST['message'] != "") {
              $_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
              if ($_POST['message'] == "") {
                  $errors .= 'Please enter a message to send.&lt;br/&gt;';
              }
          } else {
              $errors .= 'Please enter a message to send.&lt;br/&gt;';
          }
  
          if (!$errors) {
              $mail_to = 'me@somewhere.com';
              $subject = 'New Mail from Form Submission';
              $message  = 'From: ' . $_POST['name'] . "\n";
              $message .= 'Email: ' . $_POST['email'] . "\n";
              $message .= 'Homepage: ' . $_POST['homepage'] . "\n";
              $message .= "Message:\n" . $_POST['message'] . "\n\n";
              mail($to, $subject, $message);
  
              echo "Thank you for your email!&lt;br/&gt;&lt;br/&gt;";
          } else {
              echo '&lt;div style="color: red"&gt;' . $errors . '&lt;br/&gt;&lt;/div&gt;';
          }
      }
  ?&gt;
  </code></pre>
  
  <p>Note, I added the <code>session_start</code> to both pages because I'd like the errors done via that instead of <code>GET</code>. </p>
  
  <p>How do I have the errors show up on <code>test.php</code> form? </p>