title: add email validation prior to submitting on php form
body: <p>I have the following simple form that I am trying to get the email validation error to 
  show up within the form to show the error prior to submitting.</p>
  
  <p>Is there a way to do this with PHP or do I have to use JSON?</p>
  
  <p>If I have to use JSON, can anyone show me how to do this?</p>
  
  <p>Thanks in advance.</p>
  
  <p>form.html:</p>
  
  <pre><code>&lt;form method="post" name="form" action="form.php"&gt;
  &lt;p&gt;Robot: &lt;input type="text" name="robot" &gt;&lt;/p&gt; 
  &lt;p&gt;Name: &lt;input type="text" name="name" &gt;&lt;/p&gt;
  &lt;p&gt;Email:    &lt;input type="email" name="email"&gt;&lt;/p&gt;
  &lt;p&gt;Phone:    &lt;input type="telephone" name="phone"&gt;&lt;/p&gt;
  &lt;p&gt;Message:  &lt;textarea name="message"&gt;&lt;/textarea&gt;&lt;/p&gt;
  &lt;p&gt;&lt;input type="submit" value="Send Form"&gt;&lt;/p&gt;
  &lt;/form&gt;
  &lt;div id="error"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>form.php</p>
  
  <pre><code>&lt;?php
  
  // send to and from
  $to = "email@example.com";
  $headers = "From: email@example.com \r\n";
  $headers .= "Reply-To: email@example.com \r\n";
  
  // form inputs
  $name = $_POST['name'];
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $message = $_POST['message'];
  $robot = $_POST['robot'];
  
  // email message
  $email_subject = "Web Contact Message";
  
  $email_body =   
  "A message from your website contact form \n\n".
  "Email: $email \n\n".
  "Phone: $phone \n\n".
  "From: $name \n\n".
  "Message: \n". 
  "$message \n";
  
  // honeypot
  if($robot)
  header( "Location: http://www.example.com/nothankyou.html" );
      else{
  
  //validate email
  if(!filter_var($email, FILTER_VALIDATE_EMAIL))
   {
   echo '&lt;div id="error"&gt;Please Enter a Valid Email&lt;/div&gt;';
   }
  else
   {
  
  // send it
   mail($to,$email_subject,$email_body,$headers); 
   header( "Location: http://www.example.com/thankyou.html" );
  
   }  
   }   
  ?&gt;
  </code></pre>