title: Form still submitting when required fields not filled out
body: <p>My contact form is suppose to require certain fields to be filled in or it will give you an error on the fields required to be filled in before it submits the form and redirects you to the thank you page. It is not working and I am a newbie coder so I am not sure on how to fix it. Any help would be greatly appreciated. Thanks and have a great day..</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>if(isset($_POST['email'])) {
   
      // EDIT THE 2 LINES BELOW AS REQUIRED
      $email_to = "hello@capebretonstartpage.com";
      $email_subject = "Comment on CapeBretonStartPage";
   header("Location: https://www.capebretonstartpage.com/thank-you/");
   
      function died($error) {
          // your error code can go here
          echo "We are very sorry, but there were error(s) found with the form you submitted. ";
          echo "These errors appear below.&lt;/br&gt;&lt;/br&gt;";
          echo $error."&lt;/br&gt;&lt;/br&gt;";
          echo "Please go back and fix these errors.&lt;/br&gt;&lt;/br&gt;";
          die();
      }
   
   
      // validation expected data exists
      if(!isset($_POST['first_name']) ||
          !isset($_POST['last_name']) ||
          !isset($_POST['subject']) ||
          !isset($_POST['email']) ||
          !isset($_POST['comments'])) {
          died('We are sorry, but there appears to be a problem with the form you submitted.');       
      }
   
       
   
      $first_name = $_POST['first_name']; // required
      $last_name = $_POST['last_name']; // required
      $email_from = $_POST['email']; // required
      $subject = $_POST['subject']; // required
      $comments = $_POST['comments']; // required
   
      $error_message = "";
      $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
   
    if(!preg_match($email_exp,$email_from)) {
      $error_message .= 'The Email Address you entered does not appear to be valid.&lt;/br&gt;';
    }
   
      $string_exp = "/^[A-Za-z .'-]+$/";
   
    if(!preg_match($string_exp,$first_name)) {
      $error_message .= 'The First Name you entered does not appear to be valid.&lt;/br&gt;';
    }
   
    if(!preg_match($string_exp,$last_name)) {
      $error_message .= 'The Last Name you entered does not appear to be valid.&lt;/br&gt;';
    }
   
   if(!preg_match($string_exp,$subject)) {
      $error_message .= 'The Subject you entered does not appear to be valid.&lt;/br&gt;';
    }
    
    if(strlen($comments) &lt; 2) {
      $error_message .= 'The Comments you entered do not appear to be valid.&lt;/br&gt;';
    }
   
    if(strlen($error_message) &gt; 0) {
      died($error_message);
    }
   
      $email_message = "Form details below.\n\n";
   
       
      function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
      }
   
       
   
      $email_message .= "First Name: ".clean_string($first_name)."\n";
      $email_message .= "Last Name: ".clean_string($last_name)."\n";
      $email_message .= "Email: ".clean_string($email_from)."\n";
      $email_message .= "Subject: ".clean_string($subject)."\n";
      $email_message .= "Comments: ".clean_string($comments)."\n";
   
  // create email headers
  $headers = 'From: '.$email_from."\r\n".
  'Reply-To: '.$email_from."\r\n" .
  'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers);  </code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>form {
  margin-top: 50px;
  }
  
  input[type=text] {
  border: 1px solid #C2C2C2;
  border-radius: 5px;
  box-shadow: 1px 1px 4px #EBEBEB;
  box-sizing: border-box;
  color: #242527; 
  font-family: Netflix Sans,Helvetica,Arial,sans-serif;
  font-size: 17px;
  font-weight: 300;
  margin: 8px 0;
  -moz-border-radius: 5px;
  -moz-box-shadow: 1px 1px 4px #EBEBEB;
  -moz-box-sizing: border-box;
  padding: 12px 20px;
  -webkit-border-radius: 5px;
  -webkit-box-shadow: 1px 1px 4px #EBEBEB;
  -webkit-box-sizing: border-box;
  width: 400px;
  }
  
  input:focus {
  border: 1px solid #196477;
  border-radius: 5px;
  -moz-border-radius: 5px;
  outline: none;
  -webkit-border-radius: 5px;
  }
  
  textarea {
  border: 1px solid #C2C2C2;
  border-radius: 5px;
  box-shadow: 1px 1px 4px #EBEBEB;
  box-sizing: border-box;
  color: #242527; 
  font-family: Netflix Sans,Helvetica,Arial,sans-serif;
  font-size: 17px;
  font-weight: 300;
  margin: 8px 0;
  -moz-border-radius: 5px;
  -moz-box-shadow: 1px 1px 4px #EBEBEB;
  -moz-box-sizing: border-box;
  padding: 12px 20px;
  -webkit-border-radius: 5px;
  -webkit-box-shadow: 1px 1px 4px #EBEBEB;
  -webkit-box-sizing: border-box;
  width: 400px;
  }
  
  textarea:focus {
  border: 1px solid #196477;
  border-radius: 5px;
  -moz-border-radius: 5px;
  outline: none;
  -webkit-border-radius: 5px;
  }
  
  input[type="submit"] {
  background-color: #8dc63f;  
  border-bottom-style: none;
  border-left-style: none;    
  border-radius: 5px;
  border-right-style: none;   
  border-top-style: none;
  box-sizing: border-box; 
  color: #ffffff; 
  font-family: Netflix Sans,Helvetica,Arial,sans-serif;
  font-size: 18px;
  font-weight: 600;
  -moz-box-sizing: border-box;    
  opacity: 1.0;
  outline: none;
  padding: 10px;  
  -webkit-box-sizing: border-box;
  width: 400px;   
  }
  
  input[type="submit"]:hover {
  background-color: #8dc63f;    
  border-bottom-style: none;
  border-left-style: none; 
  border-radius: 5px;
  border-right-style: none;
  border-top-style: none;
  box-sizing: border-box; 
  color: #ffffff;  
  font-family: Netflix Sans,Helvetica,Arial,sans-serif;
  font-size: 18px;
  font-weight: 600;
  -moz-box-sizing: border-box; 
  opacity: 0.9;
  padding: 10px;    
  -webkit-box-sizing: border-box;
  width: 400px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form name="contactform" method="post" action="send_contact.php"&gt;
  &lt;label for="first_name"&gt;First Name: &lt;font color="red"&gt;*&lt;/font&gt;&lt;/label&gt;
  &lt;/br&gt;
  &lt;input type="text" id="first_name" name="first_name" autocomplete="off"/&gt;
  &lt;/br&gt;
  &lt;/br&gt;
  &lt;label for="last_name"&gt;Last Name: &lt;font color="red"&gt;*&lt;/font&gt;&lt;/label&gt;
  &lt;/br&gt;
  &lt;input type="text" id="last_name" name="last_name" autocomplete="off"/&gt;
  &lt;/br&gt;
  &lt;/br&gt;
  &lt;label for="email"&gt;Email: &lt;font color="red"&gt;*&lt;/font&gt;&lt;/label&gt;
  &lt;/br&gt;
  &lt;input type="text" id="email" name="email" autocomplete="off"/&gt;
  &lt;/br&gt;&lt;/br&gt;
  &lt;label for="subject"&gt;Subject: &lt;font color="red"&gt;*&lt;/font&gt;&lt;/label&gt;
  &lt;/br&gt;
  &lt;input type="text" id="subject" name="subject" autocomplete="off"/&gt;
  &lt;/br&gt;
  &lt;/br&gt;
  &lt;label for="comments"&gt;Comments: &lt;font color="red"&gt;*&lt;/font&gt;&lt;/label&gt;
  &lt;/br&gt;
  &lt;textarea name="comments" cols="50" rows="8" id="comments" autocomplete="off"/&gt;&lt;/textarea&gt;
  &lt;/br&gt;
  &lt;/br&gt;
  &lt;input type="submit" name="Submit" value="Submit"&gt;
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>