title: How do I make my PHP form errors show on the same page?
body: <p>I have a simple contact form with some PHP validation attached to it. However, when a field if left blank and the form is thrown one of these errors, it redirects to a blank page and just echos out the error on a blank screen. How would I go about keeping these errors on the same page? The errors don't have to validate instantly, just when someone clicks send. Optimally it would just direct to the contact form page and have an added string of text about the form that tells the user that there was an error.</p>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = trim($_POST["name"]);
  $email = trim($_POST["email"]);
  $subject = trim($_POST["subject"]);
  $message = trim($_POST["message"]);
  if ($name == "") {
      // header("Location: contact.php");
      echo "You must speciy a value for name.";
      exit;
  }
  if ($email == "") {
      // header("Location: contact.php");
      echo "You must speciy a value for email.";
      exit;
  }
  if ($subject == "") {
      // header("Location: contact.php");
      echo "You must speciy a value for subject.";
      exit;
  }
  if ($message == "") {
      // header("Location: contact.php");
      echo "You must speciy a value for message.";
      exit;
  }
  foreach( $_POST as $value ) {
      if( stripos($value,'Content-Type:') !== FALSE ){
          echo "There was a problem with the information you entered.";
          exit;
      }
  }
  if ($_POST["address"] != "") {
      echo "Your submission has an error.";
      exit;
  }
  
  require_once("class.phpmailer.php");
  $mail = new PHPMailer();
  
  if (!$mail-&gt; ValidateAddress($email)){
    echo "You must specify a valid email address";
    exit;
  }
  
  $email_body = "";
  $email_body = $email_body . "Name: " . $name . "&lt;br&gt;";
  $email_body = $email_body . "Email: " . $email . "&lt;br&gt;";
  $email_body = $email_body . "Subject: " . $subject . "&lt;br&gt;";
  $email_body = $email_body . "Message: " . $message . "&lt;br&gt;";
  
  $mail-&gt;setFrom($email, $name);
  $mail-&gt;addAddress('email@gmail.com', 'Staff');
  $mail-&gt;Subject = "Contact Form | " . $name;
  $mail-&gt;msgHTML($email_body);
  
  //send the message, check for errors
  if (!$mail-&gt;send()) {
      echo "There was a problem sending the email: " . $mail-&gt;ErrorInfo;
      exit;
  }
  header("Location: contact.php?status=thanks");
  exit;
  }
  ?&gt;
  </code></pre>
  
  <p>HTML</p>
  
  <pre><code>&lt;div id="contact-form"&gt;
  &lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-6"&gt;
              &lt;form class="form" method="post"&gt;
                              &lt;p class="name"&gt;
                              &lt;div id="label"&gt;&lt;label for="name"&gt;Name&lt;/label&gt;&lt;/div&gt;
                              &lt;input type="text" name="name" id="name" placeholder="John Doe" /&gt;
              &lt;/p&gt;
  
              &lt;p class="email"&gt;
              &lt;div id="label"&gt;&lt;label for="email"&gt;Email&lt;/label&gt;&lt;/div&gt;
                          &lt;input type="text" name="email" id="email" placeholder="mail@example.com" /&gt;
              &lt;/p&gt;
  
              &lt;p class="subject"&gt;
                      &lt;div id="label"&gt;&lt;label for="subjext"&gt;Subject&lt;/label&gt;&lt;/div&gt;
              &lt;select name="subject" id="subject"&gt;
              &lt;option&gt;&lt;/option&gt;
              &lt;option&gt;Request for Consultation&lt;/option&gt;
              &lt;option&gt;Ordering a Service&lt;/option&gt;
              &lt;option&gt;Just to Say Hello&lt;/option&gt;
              &lt;option&gt;Other&lt;/option&gt;
              &lt;/select&gt;
              &lt;/p&gt;
  
              &lt;p class="message"&gt;
              &lt;div id="label"&gt;&lt;label for="message"&gt;Message&lt;/label&gt;&lt;/div&gt;
                              &lt;textarea name="message" id="message" placeholder="Write something to us" /&gt;&lt;/textarea&gt;
              &lt;/p&gt;
  
                              &lt;div id="label" class="address" style="display:none !important"&gt;&lt;label for="address"&gt;Email&lt;/label&gt;
                          &lt;input type="text" name="address" id="address" placeholder="123 Elm Street" /&gt;
                  &lt;p&gt;If you're a human, please leave this field blank.&lt;/p&gt;
                  &lt;/div&gt;
  
              &lt;p class="submit"&gt;
              &lt;input type="submit" value="Send" class="btn-blue btn-submit"/&gt;
              &lt;/p&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>