title: Email validation using PHP
body: <p>I have a form for which i am trying to validate the email address. If the email address is incorrect i want a value of "Please type a valid email address." to be returned into the "email" input box on the form. What am i doing wrong? No validation is taking place. I receive the form information at my email and once submitted the user is sent to the "Thank you" page, but no validation. I can put anything in the "email" input and the form will submit.</p>
  
  <pre><code>&lt;form action="../php/contact.php" method="post"&gt;
    &lt;p&gt;First Name:&lt;/p&gt;
     &lt;input class="box_style" type="text" name="first_name" required maxlength="20" /&gt;
  
    &lt;p&gt;Last Name:&lt;/p&gt;
     &lt;input class="box_style" type="text" name="last_name" required maxlength="25" /&gt;
  
    &lt;p&gt;Email:&lt;/p&gt;
     &lt;input class="box_style" type="text" name="email" required maxlength="50" /&gt;
  
     &lt;p&gt;Contact Number (optional):&lt;/p&gt;
     &lt;input class="box_style" type="text" name="contact_number" maxlength="12" /&gt;
  
    &lt;p&gt;How did you find us?&lt;/p&gt;
    &lt;select class="box_style" name="how" required&gt;
      &lt;option value="choose"&gt;Select...&lt;/option&gt;
      &lt;option value="referal"&gt;Referal&lt;/option&gt;
      &lt;option value="website"&gt;Website&lt;/option&gt;
      &lt;option value="search"&gt;Search Engine&lt;/option&gt;
      &lt;option value="card"&gt;Business Card&lt;/option&gt;
   &lt;/select&gt; 
  
   &lt;p&gt;Enquiries:&lt;/p&gt; 
    &lt;textarea class="box_style" name="inquiries" cols="30" rows="10"&gt;&lt;/textarea&gt;
  
    &lt;input class="box_style" type="submit" name="submit" value="Submit"/&gt;   
   &lt;/form&gt;
  </code></pre>
  
  <p>and this is the php</p>
  
  <pre><code>&lt;?php
  
  function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
  }
  
  // define variables and set to empty values
  $fName = $lName = $email = $cNum = $how =  $enquiries = "";
  
  if ($_SERVER["REQUEST_METHOD"] == "POST") { 
  $fName = test_input($_POST["first_name"]);
  $lName = test_input($_POST["last_name"]);
  $cNum = test_input($_POST["contact_number"]);
  $how = test_input($_POST["how"]);
  $enquiries = test_input($_POST["enquiries"]);
  
  $email = test_input($_POST["email"]);
  if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      print  "&lt;p&gt;Please type a valid email address.&lt;/p&gt;";
  
  header("Location: www.mysite/thankyou.com");
  }};
  
  $email_from = 'my@email.com';
  $email_subject = "New Inquiry";
  $email_body = "You have received a new message from" ." ". "$fName" ." ".    "$lName" ."\n".
  "$inquiries"."\n".
  "Referal Type:" ." ". "$how" ."\n".
  "Contact Number:" ." ". "$cNum" ."\n";
  
  $to = "my@email.com";
  $headers = "From: $email_from \r\n";
  $headers .= "Reply-To: $email \r\n";
  mail($to,$email_subject,$email_body,$headers);
  
  
  ?&gt;
  </code></pre>
  
  <p>Thank You</p>