title: Checkbox Validation and Post PHP, quick one :)
body: <p>I need to check that the check box is checked (required) and need the validation to perform much like the other form fields, and then i need to post the value in email.</p>
  
  <p>PHP for entire form as it stands:</p>
  
  <pre><code>&lt;?php
  if(isset($_POST['formtrigger'])):
  
      //config
      define('FORM_SENDTO','hello@domain.com');
      define('FORM_SUBJECTLINE','Enquiry from website');
      define('ERR_MSG_FIELD_REQUIRED','This field is required.');
      define('ERR_MSG_FIELD_INVALIDEMAIL','Please enter a valid email address.');
  
      //setup validation rules
  
          //Name
          $validation_rules['forename']['required'] = true;
          $validation_rules['surname']['required'] = true;
  
          //Company
          $validation_rules['company']['required'] = true;
  
          //Address
          $validation_rules['address']['required'] = true;
  
          //Tel
          $validation_rules['tel']['required'] = true;
  
          //Email
          $validation_rules['email']['required'] = true;
          $validation_rules['email']['valid_email'] = true;
  
          //Enquiry
          $validation_rules['enquiry']['required'] = true;
  
          //title/gender
          $validation_rules['ts1']['required'] = true;
  
          $validation_rules['ts2']['required'] = true;
  
      //validate the form
      $formerrors=0;
      foreach($_POST as $formfield_name=&gt;$formfield_value):
  
          //set the entered value as a sanitised string
          $_POST_SANITISED[$formfield_name] = filter_var($formfield_value,FILTER_SANITIZE_STRING);
  
          //Check if required
          if($validation_rules[$formfield_name]['required']):
              if(!strlen($formfield_value)&gt;0):
                  $formerrors++;
                  $fielderrors[$formfield_name][] = ERR_MSG_FIELD_REQUIRED;
              endif;
          endif;
  
          //Check if valid email required
          if($validation_rules[$formfield_name]['valid_email']):
              if(!filter_var($formfield_value,FILTER_VALIDATE_EMAIL)):
                  $formerrors++;
                  $fielderrors[$formfield_name][] = ERR_MSG_FIELD_INVALIDEMAIL;
              endif;
          endif;
  
      endforeach;
  
      //process form and send message if validation passed
      if($formerrors==0):
          $email_msg[] = "New general enquiry\n\n-----------\n";
          $email_msg[] = "Title: ".$_POST_SANITISED['ts1']."\n";
          $email_msg[] = "Gender: ".$_POST_SANITISED['ts2']."\n";
          $email_msg[] = "Forename: ".$_POST_SANITISED['forename']."\n";
          $email_msg[] = "Surname: ".$_POST_SANITISED['surname']."\n";
          $email_msg[] = "Company: ".$_POST_SANITISED['company']."\n";
          $email_msg[] = "Address: ".$_POST_SANITISED['address']."\n";
          $email_msg[] = "Telephone No.: ".$_POST_SANITISED['tel']."\n";
          $email_msg[] = "Email: ".$_POST_SANITISED['email']."\n";
          $email_msg[] = "Enquiry: ".$_POST_SANITISED['enquiry']."\n";
  
          $email_msg[] = "-----------\n";
  
          $email_msg = implode('',$email_msg);
  
          $email_headers = 'From: ' . $_POST_SANITISED['email'] . "\r\n" .
          'Reply-To: ' . $_POST_SANITISED['email'] . "\r\n" .
          'X-Mailer: PHP/' . phpversion();
  
          mail(FORM_SENDTO,FORM_SUBJECTLINE,$email_msg,$email_headers);
  
          header('Location: ?msgsent=1#thanks');
  
      endif;
  
  endif;
  
  function errorOutput($fieldname=''){
      global $fielderrors;
      if(count($fielderrors[$fieldname])&gt;0):
          foreach($fielderrors[$fieldname] as $err_msg):
              $error_str .= '&lt;div class="form-fielderror-msg"&gt;'.$err_msg.'&lt;/div&gt;';
          endforeach;
      endif;
      return $error_str ? $error_str : false;
  }
  
  function errorClass($fieldname=''){
      global $fielderrors;
      $error_class = '';
      if(count($fielderrors[$fieldname])&gt;0):
         $error_class = 'form-fielderror';
      endif;
      return $error_class ? $error_class : false;
  }
  </code></pre>
  
  <p>?></p>
  
  <p>Here is the HTML:</p>
  
  <pre><code>        &lt;?php if($_GET['msgsent']==1): ?&gt;
  
      &lt;h1&gt;Thanks for your enquiry. If requested, we will get in touch shortly.&lt;/h1&gt;
  
      &lt;?php else: ?&gt;
  
  
      &lt;div id="form-cont"&gt;
  
          &lt;form action="&lt;?=$_SERVER['PHP_SELF']?&gt;" method="post"&gt;
  
  
  
              &lt;div class="form-element"&gt;
                  &lt;label for="ts1"&gt;Title:*&lt;/label&gt;
                  &lt;select name="ts1" class="&lt;?=errorClass('ts1')?&gt;"&gt;
                          &lt;option value=""&gt;-- Please select --&lt;/option&gt;
                          &lt;option value="Mr" &lt;?=$_POST['ts1']=='Mr' ? 'selected="selected"' : '' ?&gt;&gt;Mr&lt;/option&gt;
                          &lt;option value="Mrs" &lt;?=$_POST['ts1']=='Mrs' ? 'selected="selected"' : '' ?&gt;&gt;Mrs&lt;/option&gt;
                          &lt;option value="Miss" &lt;?=$_POST['ts1']=='Miss' ? 'selected="selected"' : '' ?&gt;&gt;Miss&lt;/option&gt;
                          &lt;option value="Ms" &lt;?=$_POST['ts1']=='Ms' ? 'selected="selected"' : '' ?&gt;&gt;Ms&lt;/option&gt;
                  &lt;/select&gt;
                  &lt;?=errorOutput('ts1')?&gt;
              &lt;/div&gt;
  
              &lt;div class="form-element"&gt;
                  &lt;label for="ts2"&gt;Gender:*&lt;/label&gt;
                  &lt;select name="ts2" class="&lt;?=errorClass('ts2')?&gt;"&gt;
                          &lt;option value=""&gt;-- Please select --&lt;/option&gt;
                          &lt;option value="Male" &lt;?=$_POST['ts2']=='Male' ? 'selected="selected"' : '' ?&gt;&gt;Male&lt;/option&gt;
                          &lt;option value="Female" &lt;?=$_POST['ts2']=='Female' ? 'selected="selected"' : '' ?&gt;&gt;Female&lt;/option&gt;
                  &lt;/select&gt;
                  &lt;?=errorOutput('ts2')?&gt;
              &lt;/div&gt;      
  
              &lt;div class="form-element"&gt;
                  &lt;label for="forename"&gt;Forename:*&lt;/label&gt;
                  &lt;input type="text" name="forename" class="textbox &lt;?=errorClass('forename')?&gt;" value="&lt;?=$_POST['forename']?&gt;" /&gt;
                  &lt;?=errorOutput('forename')?&gt;
              &lt;/div&gt;
  
              &lt;div class="form-element"&gt;
                  &lt;label for="surname"&gt;Surname:*&lt;/label&gt;
                  &lt;input type="text" name="surname" class="textbox &lt;?=errorClass('surname')?&gt;" value="&lt;?=$_POST['surname']?&gt;" /&gt;
                  &lt;?=errorOutput('surname')?&gt;
              &lt;/div&gt;
  
              &lt;div class="form-element"&gt;
                  &lt;label for="company"&gt;Company:*&lt;/label&gt;
                  &lt;input type="text" name="company" class="textbox &lt;?=errorClass('company')?&gt;" value="&lt;?=$_POST['company']?&gt;" /&gt;
                  &lt;?=errorOutput('company')?&gt;
              &lt;/div&gt;
  
              &lt;div class="form-element"&gt;
                  &lt;label for="address"&gt;Address:*&lt;/label&gt;
                  &lt;input type="text" name="address" class="textbox &lt;?=errorClass('address')?&gt;" value="&lt;?=$_POST['address']?&gt;" /&gt;
                  &lt;?=errorOutput('address')?&gt;
              &lt;/div&gt;
  
              &lt;div class="form-element"&gt;
                  &lt;label for="tel"&gt;Telephone No:*&lt;/label&gt;
                  &lt;input type="text" name="tel" class="textbox &lt;?=errorClass('tel')?&gt;" value="&lt;?=$_POST['tel']?&gt;" /&gt;
                  &lt;?=errorOutput('tel')?&gt;
              &lt;/div&gt;
  
              &lt;div class="form-element"&gt;
                  &lt;label for="email"&gt;Email:*&lt;/label&gt;
                  &lt;input type="text" name="email" class="textbox &lt;?=errorClass('email')?&gt;" value="&lt;?=$_POST['email']?&gt;" /&gt;
                  &lt;?=errorOutput('email')?&gt;
              &lt;/div&gt;  
  
              &lt;div class="form-element"&gt;
                  &lt;label for="enquiry"&gt;Your Enquiry:*&lt;/label&gt;
                  &lt;textarea name="enquiry" class="&lt;?=errorClass('enquiry')?&gt;"&gt;&lt;?=$_POST['enquiry']?&gt;&lt;/textarea&gt;
                  &lt;?=errorOutput('enquiry')?&gt;
              &lt;/div&gt;      
  
              &lt;div class="form-element"&gt;
                  &lt;label for="terms"&gt;Terms and Conditions&lt;/label&gt;
                  &lt;input type="checkbox" name="terms" class="checkbox &lt;?=errorClass('terms')?&gt;" value="&lt;?=$_POST['terms']=1?&gt;" /&gt;
                  &lt;?=errorOutput('terms')?&gt;
              &lt;/div&gt;      
  
              &lt;div class="form-element"&gt;
              &lt;p class="clear"&gt;* denotes required field.&lt;/p&gt;
              &lt;input type="submit" class="submit" name="submit" value="Send" alt="Submit" title="Submit" /&gt;
              &lt;/div&gt;
              &lt;input type="hidden" name="formtrigger" value="1" /&gt;
          &lt;/form&gt;
  
      &lt;/div&gt;
      &lt;?php endif ?&gt;  
  </code></pre>
  
  <p>The checkbox in question as at the bottom:</p>
  
  <pre><code>&lt;div class="form-element"&gt;
              &lt;label for="terms"&gt;Terms and Conditions&lt;/label&gt;
              &lt;input type="checkbox" name="terms" class="checkbox &lt;?=errorClass('terms')?&gt;" value="&lt;?=$_POST['terms']=1?&gt;" /&gt;
              &lt;?=errorOutput('terms')?&gt;
          &lt;/div&gt; 
  </code></pre>
  
  <p>Many thanks for you help! :)</p>