title: redirect to new page after form submitted and validated using php
body: <p>I have been trying to improve our contact forms with php validation, I can get the form to be validated and show errors when each field is not filled in correctly, success message appears when the form is completed and validated correctly, and an email is sent to me with the information from the form.</p>
  
  <p>My problem is that I can not get the header('location'...) part of the code to work. Rather than just having "Form has been submitted successfully" appear underneath the form once submitted, I would like it to go to a "Thank You" page instead.</p>
  
  <p>Here is my code:</p>
  
  <p>This is this my form page:</p>
  
  <pre><code>&lt;?php 
  include('validate.php'); 
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;title&gt;TEST&lt;/title&gt;
  &lt;style&gt;    
      input, textarea {font-size: 1em;}
      p.error {background: #ffd; color: red;}
      p.error:before {content: "Error:";}
      p.success {background: #ffd; color: green;}
      p.success:before {content: "Success:";}
      p.error, p.success {font-weight: bold;}
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h2&gt;Please fill up the form below and submit.&lt;/h2&gt;
  &lt;?=$error?&gt;
  &lt;form action="html_form_to_validate.php" method="post"&gt;
  &lt;table&gt;
  &lt;tr&gt;
     &lt;td&gt;Name: &lt;/td&gt;
  &lt;td&gt;
  &lt;input type="text" name="name" placeholder="Name *(Required)" value="&lt;?=@$name?&gt;"/&gt;    &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td&gt;Company: &lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="company" placeholder="Company" value="&lt;?=@$company?&gt;"/&gt;    &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Hear: &lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="hear"  placeholder="How did you hear about us?" value="&lt;?=@$hear?&gt;"/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Email: &lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="email" value="&lt;?=@$email?&gt;"/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Phone: &lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="phone" value="&lt;?=@$phone?&gt;"/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Message: &lt;/td&gt;
  &lt;td&gt;&lt;textarea name="comments"&gt;&lt;?=@$comments?&gt;&lt;/textarea&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;            
  &lt;input type="submit" name="submit" value="Submit"/&gt; &lt;input type="reset" name="reset" value="Reset"/&gt;
  &lt;/form&gt;
  
  &lt;?php
  if (isset($_POST['submit']) &amp;&amp; $error == '') { // if there is no error, then process further
  echo "&lt;p class='success'&gt;Form has been submitted successfully.&lt;/p&gt;"; // showing success message
  
  $name=$_POST['name']; 
  $company=$_POST['company']; 
  $telephone=$_POST['telephone']; 
  $email=$_POST['email']; 
  $hear=$_POST['hear']; 
  $comments=$_POST['comments']; 
  
  $to="chris@example.com";
  $subject="Request for information";
  $from="sales@example.com";
  $message="&lt;html&gt;
  &lt;body topmargin='0'&gt;
  
  &lt;div align='center'&gt;
  &lt;table border='0' width='736' id='table1' cellspacing='0' cellpadding='0'&gt;
  &lt;tr&gt;
  &lt;td height='129' bgcolor='#EDEDE9' width='736' valign='top' style='margin-left: 10'&gt;
  &lt;table border='0' id='table2' cellspacing='10' cellpadding='15' width='726'&gt;
  &lt;tr&gt;
  &lt;td width='676' valign='top' bgcolor='#FFFFFF'&gt;
  &lt;p align='left'&gt;
  &lt;img border='0' src='http://www.example.com/images/logo.png' align='left' hspace='0'&gt;    &lt;/p&gt;
  &lt;p align='left'&gt;
  &lt;br&gt;&lt;br&gt;
  &lt;b&gt;
  &lt;font face='Verdana' color='#0078c1' style='font-size: 20pt'&gt;
  &amp;nbsp;&amp;nbsp;Request for information&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;
  &lt;p align='left'&gt;&amp;nbsp;&lt;/p&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td width='676' valign='top' bgcolor='#FFFFFF'&gt;
  &lt;p&gt;
  &lt;font face='Verdana' size='2'&gt;The following person has been on &lt;a href='http://www.example.com'&gt;
  &lt;font color='#0078c1'&gt;www.example.com&lt;/font&gt;&lt;/a&gt; and requesting information from our 'contact form'.&lt;/font&gt;&lt;/p&gt;
  &lt;p&gt;
  &lt;font face='Verdana' size='2'&gt;Name: &lt;/font&gt;&lt;font face='Verdana' size='2'&gt;&lt;b&gt;$name&lt;/b&gt;    &lt;/font&gt;&lt;/p&gt;
  &lt;p&gt;
  &lt;font face='Verdana' size='2'&gt;Company: &lt;/font&gt;&lt;font face='Verdana' size='2'&gt;&lt;b&gt;$company&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;
  &lt;p&gt;
  &lt;font face='Verdana' size='2'&gt;Telephone: &lt;font face='Verdana' size='2'&gt;&lt;b&gt;$telephone&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;
  &lt;p&gt;
  &lt;font face='Verdana' size='2'&gt;Email: &lt;font face='Verdana' size='2'&gt;&lt;b&gt;$email&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;
  &lt;p&gt;
  &lt;font face='Verdana' size='2'&gt;Heard about us from: &lt;/font&gt;&lt;font face='Verdana' size='2'&gt;&lt;b&gt;$hear&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;
  &lt;p&gt;
  &lt;font face='Verdana' size='2'&gt;Message: &lt;font face='Verdana' size='2'&gt;&lt;b&gt;$comments&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  ";
  $headers = "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
  $headers  .= "From: $from\r\n"; 
  @mail($to, $subject, $message, $headers);
  header('Location: http://www.example.com/contact/thank-you.php'); 
  }
  ?&gt; 
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And my validate.php file:</p>
  
  <pre><code>&lt;?php
  
  $error = ""; // Initialize error as blank
  
  if (isset($_POST['submit'])) { // check if the form is submitted
  #### removing extra white spaces &amp; escaping harmful characters ####
  $name             = trim($_POST['name']);
  $company         = trim($_POST['company']);
  $hear             = trim($_POST['hear']);
  $email                 = $_POST['email'];
  $phone                = $_POST['phone'];
  $comments                = $_POST['comments'];
  
  #### start validating input data ####
  #####################################
  
  # Validate Name #
  
      // if name is not 3-20 characters long, throw error
      if (strlen($name) &lt; 3 OR strlen($name) &gt; 20) {
          $error .= '&lt;p class="error"&gt;Name should be within 3-20 characters long.&lt;/p&gt;';
      }
  
  # Validate Email #
      // if email is invalid, throw error
      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { // you can also use regex to do same
          $error .= '&lt;p class="error"&gt;Enter a valid email address.&lt;/p&gt;';
      }
  
  # Validate Phone #
      // if phone is invalid, throw error
      if (!ctype_digit($phone) OR strlen($phone) &lt; 9) {
          $error .= '&lt;p class="error"&gt;Enter a valid telephone number.&lt;/p&gt;';
      }
  
  # Validate Comments #
      if (strlen($comments)==0 OR strlen($comments)&gt;240) {
          $error .= '&lt;p class="error"&gt;Please enter your message less than 240 characters.&lt;/p&gt;';
      }
  
  #### end validating input data ####
  #####################################
  }
  </code></pre>
  
  <p>I am a novice and have used scripts from other places to do what I wanted to do - so be nice lol</p>
  
  <p>Thanks</p>
  
  <p>Chris</p>