title: Required alert on PHP contact form
body: <p>I've been looking around for ages and haven't found an easy way of editing this code so that when the user hasn't typed anything into a required field (all of the fields are required) an alert is shown asking the user to please enter something.</p>
  
  <p>Can anyone help?</p>
  
  <pre><code>&lt;?php
      $field_name = $_POST['name'];
      $field_email = $_POST['email'];
      $field_message = $_POST['message'];
  
      $mail_to = 'you@yourdomain.com';
      $subject = 'Site Mail';
  
      $body_message = 'From: '.$field_name."\n";
      $body_message .= 'Email: '.$field_email."\n";
      $body_message .= 'Message: '.$field_message;
  
      $headers = 'From: '.$field_email."\r\n";
      $headers .= 'Reply-To: '.$field_email."\r\n";
  
      $mail_status = mail($mail_to, $subject, $body_message, $headers);
  
      if ($mail_status) { ?&gt;
          &lt;script language="javascript" type="text/javascript"&gt;
              alert('Thanks! Your email has been sent.');
              window.location = 'index.html';
          &lt;/script&gt;
      &lt;?php
      }
      else { ?&gt;
          &lt;script language="javascript" type="text/javascript"&gt;
              alert('Sorry, something went wrong.');
              window.location = 'index.html';
          &lt;/script&gt;
      &lt;?php
      }
  ?&gt;
  </code></pre>