title: How can I display form validation messages on the same page as the form itself?
body: <p>I have a simple html form with a single field that submits to download.php which validates the form and finally sends me back an email with the user input.  So far, everything works great - but I would like the error messages and thank you message to display inside the original html form, not on a new page (as is happening now).</p>
  
  <p>Can you tell me how to keep myform.html visible after the submit button is clicked, and load the error and thank you messages inside the "notification" div?</p>
  
  <p>BTW, just in case it's relevant, myform.html is dynamically displayed inside a  on index.html using jQuery .load().</p>
  
  <p>Thanks for your help!</p>
  
  <p>myform.html</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;...&lt;/head&gt;
  &lt;body&gt;
  
      &lt;div id="notification"&gt;&lt;/div&gt; &lt;!-- want to load errors and thank you, here --&gt;
  
      &lt;form id="downloadForm" method="post" action="_php/download.php"&gt;
  
          &lt;label for="biz_email"&gt;Business Email:&lt;/label&gt;&lt;input type="text" name="biz_email"&gt;
  
          &lt;div id="submit" class="submit_button"&gt;&lt;/div&gt;
          &lt;!-- FYI submit function called in separate jQuery file --&gt;
  
      &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>download.php</p>
  
  <pre><code>&lt;?php
  if(isset($_POST['biz_email'])) {
  
  
      $email_to = "foo@bar.com"; //my email
      $email_subject = "form submission";
      $email_from ="form test";
  
      $biz_email = $_POST['biz_email'];
      $error_message = "";
  
  
      // validation
      ...
  
  
      //error code 
      function died($error) {
          echo "There are errors in your submission:&lt;br /&gt;&lt;br /&gt;";
          echo $error."&lt;br /&gt;&lt;br /&gt;";
          echo "Please go back and fix these errors.&lt;br /&gt;&lt;br /&gt;";
          die();
      }
  
  
      //email message (sent back to me)
      $email_message = "Form details below.\n\n";
  
      function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
      }
  
      $email_message .= "Email: ".clean_string($biz_email)."\n";
  
      // create email headers
      $headers = 'From: '.$biz_email."\r\n".
      'Reply-To: '.$biz_email."\r\n" .
      'X-Mailer: PHP/' . phpversion();
  
      @mail($email_to, $email_subject, $email_message, $headers);  
      ?&gt;
  
      &lt;!-- thank you message --&gt;
  
      Success!
  
  &lt;?php
  }
  ?&gt;
  </code></pre>