title: Validate a HTML Contact Form
body: <p>I have a HTML Contact Form which I need validating in a certain way. </p>
  
  <p>Instead of alerting the user in JavaScript if any fields have not been filled I prefer to show the words 'Field Required' below the fields which were left empty.</p>
  
  <p>Here is my contact form <code>contact.html</code>:</p>
  
  <pre><code>&lt;form id = "myForm" action="contact.php" method="post"&gt;
  
  &lt;label for="name"&gt;Name:&lt;/label&gt;
  &lt;input type="text" name="name" id="name" value="" tabindex="1" /&gt;
  
  
  &lt;label for="name"&gt;Email:&lt;/label&gt;
  &lt;input type="text" name="email" id="email" value="" tabindex="1" /&gt;
  
  
  &lt;input type="submit" value="Submit" class="button" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Here is my PHP form <code>contact.php</code> (Everything omitted apart from validation):</p>
  
  <pre><code>//Validation... I am building up an error message string to alert the user at the end (omitted) 
  
  $errormessage = '';
  
  if(!isset($_POST['name']) || strlen($_POST['name']) &lt; 1){
  $errormessage .= 'You have not entered your Name\n';
  }
  
  if(!isset($_POST['email']) || strlen($_POST['email']) &lt; 1){
  $errormessage .= 'You have not entered your Email Address\n';
  }
  
  if($errormessage != ''){ ?&gt;
  
  &lt;script language="javascript" type="text/javascript"&gt;
      alert('&lt;?php echo "$errormessage" ?&gt;');
      window.location = 'contact.html';
  &lt;/script&gt;
  
  &lt;?php } 
  
  
  else {
  
  $mail_to = 'info@email.co.uk';
  $subject = 'Message from a site visitor '.$field_name;
  
  $body_message = 'From: '.$field_name."\n\n";
  $body_message .= 'E-mail: '.$field_email."\n\n";
  
  
  
  $headers = 'From: '.$field_email."\r\n";
  $headers .= 'Reply-To: '.$field_email."\r\n";
  
  $mail_status = mail($mail_to, $subject, $body_message, $headers);
  
  }
  </code></pre>
  
  <p>As you can see my current validation builds up a string in PHP which is going to be alerted to the user at the end (omitted) however I want to replace this method by showing the string 'Field Required' below the HTML fields which have been left empty.</p>