Checkboxes in Form to Email

Hi,

I have a php form which has checkboxes in. When the resulting email gets sent if a user has selected more than one check box it doesn’t get posted in the email, any suggestions to my code?[php]<?php

function spamcheck($field)

{

$field=filter_var($field, FILTER_SANITIZE_EMAIL);

if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}

function validate_form()
{
	$errors = array();
	
	if(!(isset($_POST['organisation']) && (strlen($_POST['organisation']) > 3))){
	$errors['organisation'] = '<span style="color:red; font-size:12px">*Please enter an organisation name</span>';
	}
	
	
	
	if(!(isset($_POST['postcode']) && (strlen($_POST['postcode']) > 3))){
	$errors['postcode'] = '<span style="color:red; font-size:12px">*Please enter your postcode</span>';
	}
	
		if(!(isset($_POST['users']) && (strlen($_POST['users']) > 0))){
	$errors['users'] = '<span style="color:red; font-size:12px">*Please enter the number of users</span>';
	}
	
	
				if(!(isset($_POST['payment']) && (strlen($_POST['payment']) > 3))){
	$errors['payment'] = '<span style="color:red; font-size:12px">*Please enter your payment method</span>';
	}
	
	
	
	
	if(!(isset($_POST['name']) && (strlen($_POST['name']) > 3))){
	$errors['name'] = '<span style="color:red; font-size:12px">*Please enter a contact name</span>';
	}
	
	if(!(isset($_POST['address']) && (strlen($_POST['address']) > 3))){
	$errors['address'] = '<span style="color:red; font-size:12px">*Please enter a valid email address</span>';
	}
	$mailcheck = spamcheck($_POST['address']);
	  if ($mailcheck==FALSE)
		{
			$errors['address'] = '<span style="color:red; font-size:12px">*Please enter a valid email address</span>';
		}


	
	if(!(isset($_POST['telephone']) && (strlen($_POST['telephone']) > 3))){
	$errors['telephone'] = '<span style="color:red; font-size:12px">*Please enter a telephone number</span>';
	}
	  
  $phone = ereg_replace('[^0-9]', '', $_POST['telephone']);

  if (!filter_var($phone, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^0[1-9][0-9]{8,9}$/")))) {

 $errors['telephone'] = '<span style="color:red; font-size:12px">*Please enter a telephone number</span>';

  } 
	
	if(!(isset($_POST['invoice']) && (strlen($_POST['invoice']) > 3))){
	$errors['invoice'] = '<span style="color:red; font-size:12px">*Please enter your invoice address</span>';
	}
	
		if(!(isset($_POST['courses']) && (strlen($_POST['courses']) > 3))){
	$errors['courses'] = '<span style="color:red; font-size:12px">*Please select your course(s)</span>';
	}
	

	return $errors;

}



function display_form($errors)
{
 $defaults['organisation']= isset($_POST['organisation']) ? htmlentities($_POST['organisation']) : '';
  $defaults['name']= isset($_POST['name']) ? htmlentities($_POST['name']) : '';
   $defaults['invoice']= isset($_POST['invoice']) ? htmlentities($_POST['invoice']) : '';
    $defaults['payment']= isset($_POST['payment']) ? htmlentities($_POST['payment']) : '';
	 $defaults['users']= isset($_POST['users']) ? htmlentities($_POST['users']) : '';
	  $defaults['courses']= isset($_POST['courses']) ? htmlentities($_POST['courses']) : '';
	   $defaults['postcode']= isset($_POST['postcode']) ? htmlentities($_POST['postcode']) : '';
 $defaults['address']= isset($_POST['address']) ? htmlentities($_POST['address']) : '';
 $defaults['telephone']= isset($_POST['telephone']) ? htmlentities($_POST['telephone']) : '';

 
 ?>
 
      
      
      

 <?php
 }
 
 function print_error($key, $errors){
 if(isset($errors[$key])){
 print "<dd class='error'>{$errors[$key]}</dd>";
 
 }
 }
 


if($_SERVER['REQUEST_METHOD']== 'POST'){
	$errors = validate_form();
	if(count($errors)){
	display_form($errors);
	}else{
		echo '<p>Thankyou...'.ucwords($_POST['name']).' Your download instructions or login details will be sent to you shortly, along with an invoice which is payable within seven days.</p>';
		//===========Email address her=========//
		$to = "[email protected]";
		$subject = "An order has been placed via the order form";
		$message = ' Orgainisation Name: '.$_POST['organisation'];
		$message.= "\n".' Contact Name: '.$_POST['name'];
		$message.= "\n".' Telephone Number: '.$_POST['telephone'];			
		$message.= "\n".' Invoice Address: '.$_POST['invoice'];	
		$message.= "\n".' Postcode: '.$_POST['postcode'];		
		$message.= "\n".' Users: '.$_POST['users'];		
		$message.= "\n".' Total Cost: '.$_POST['cost'];		
		$message.= "\n".' Payment: '.$_POST['payment'];		
		$message.= "\n".' Message: '.$_POST['message'];
		$message.= "\n".' Courses: '.$_POST['courses'];
		


		$from = $_POST['address'];
		$headers = "From: $from";
		mail($to,$subject,$message,$headers);
	}
}else {
display_form($errors);
}

?>[/php]

[code]

      <table width="100%" border="0" cellpadding="2" cellspacing="10"  >
        <tr>
          <td width="53%" colspan="2" valign="top"><label for="organisation"><strong>Organisation Name:*</strong></label></td>
         </tr>
         <tr> 
          <td width="47%" colspan="2"> <input type='text' name='organisation' id="organisation" value='<?php echo $defaults['organisation']?>'/></td></tr>
	    <tr>             
       
         <td colspan="2"><?php print_error('organisation',$errors)?></td>
        </tr>
        
        <tr>
          <td width="53%" colspan="2" valign="top"><label for="name"><strong>Contact Name:*</strong></label></td></tr>
          <tr>
          <td width="47%" colspan="2"> <input type='text' name='name' id="name" value='<?php echo $defaults['name']?>'/></td>
            <tr>             

         <td colspan="2"> <?php print_error('name',$errors)?></td>
			
			
        </tr>
        <tr>
          <td colspan="2" valign="top"><label for="phone">
          <strong>Telephone Number:* </strong></label></td>
          </tr>
          <tr><td colspan="2"> <input type='text' id="phone" name='telephone' value='<?php echo $defaults['telephone']?>'/></td>
            <tr>             

         <td colspan="2"> <?php print_error('telephone',$errors)?></td>
        </tr>
        <tr>
          <td colspan="2" valign="top"><label for="email">
            <strong>Email Address:* </strong><em><br />
          Note course information and your invoice will be sent here</em></label></td></tr>
          <tr><td colspan="2"><input type='text' name='address' id="email" value='<?php echo $defaults['address']?>'/></td>
            <tr>             

         <td colspan="2"> 
              <?php print_error('address',$errors)?></td>
        </tr>
        
        <tr>
          <td colspan="2" valign="top"><label for="invoice">
          <strong>Invoice Address:*</strong></label></td></tr>
          <tr>
          <td colspan="2" valign="top"><textarea rows="6" cols="35" name="invoice" id="invoice">
		<?php echo $defaults['invoice']?></textarea>
		 </td>
            <tr>             

         <td colspan="2">  <?php print_error('invoice',$errors)?></td>

        </tr>
        
           <tr>
          <td width="53%" colspan="2" valign="top"><label for="postcode"><strong>Postcode:*</strong></label></td></tr><tr>
          <td width="47%" colspan="2"> <input type='text' name='postcode' id="postcode" value='<?php echo $defaults['postcode']?>'/>
         </td>
            <tr>             
    
         <td colspan="2">    <?php print_error('postcode',$errors)?></td>
			
			
        </tr>
        
           <tr>
          <td width="53%" colspan="2" valign="top"><label for="users"><strong>Number of Users:*</strong><em> <br />
          Enter 'Site' if you would like to purchase the   course(s) on a site licence basis (only available on selected courses)</em></label></td></tr>
          <tr>
          <td width="47%" colspan="2"> <input type='text' name='users' id="users" size="6" value='<?php echo $defaults['users']?>'/>
         </td>
        <tr>             
 
         <td colspan="2">    <?php print_error('users',$errors)?></td>
			
			
       </tr>
        
          
        
         <tr>
          <td width="53%" colspan="2" valign="top"><label for="cost"><strong>Total Cost (if known):</strong></label></td></tr>
          <tr>
          <td width="47%" colspan="2"> <input type='text' name='cost' id="cost" value='<?php echo $defaults['cost']?>'/>
     </td>
			
			
        </tr>  <tr>
         <td colspan="2">&nbsp;</td></tr>
        
          
      <tr>
        <td width="30%" colspan="2" valign="top"><label for="payment"><strong>Payment Method:*</strong> <br />
            <em>Please note that if you select PayPal an invoice will be sent via email. </em></label></td></tr>
        <tr>
        <td colspan="2"><input type='radio' name='payment' value='bacs'/>
        BACS
          <input type='radio' name='payment' value='cheque'/>
        Cheque
     
          <input type='radio' name='payment' value='paypal'/>
          PayPal (used for card payments)</td>
                
        <tr>             
       
         <td colspan="2">  <?php print_error('payment',$errors)?></td>
     </tr>
        
         <tr>
          <td colspan="2" valign="top"><label for="message">
          <strong>Additional Comments:</strong><br />
          <em>For example, if you are purchasing one course on a site licence basis and another per user, please state this here</em></label></td></tr><tr>
          <td colspan="2" valign="top"><textarea rows="7" cols="35" name="message" id="message">
		<?php echo $defaults['message']?></textarea>
		 </td>

        </tr>
        
         <tr><td colspan="2">&nbsp;</td>
  </tr>
         <tr>
          <td colspan="2" valign="top"><label for="message">
          <strong>Course(s):*<br />
          </strong><em>Please select at least one  course. If you wish to select more   than one course hold done the Ctrl key on your keyboard and click on the courses   that you wish to purchase. </em> </label></td></tr><tr>
          <td colspan="2" valign="top">

Health & Safety Essentials – Introduction to Working Safely

First Aid Display Screen Equipment and Office Ergonomics Equality and Diversity Preventing Underage Sales Slips, Trips and Falls In the Line of Fire - Commerce and Industry Fire safety Risk Assessment Forms Fire Safety in Childcare Settings In the Line of Fire - Schools Childcare Professionals Learning Pack Food Hygiene - The Basics Infection Control for Childcare Professionals Introduction to Childhood IIlnesses Manual Handling for Childcare Professionals Safe and Effective Use of an EpiPen Safe Handling of Medicines in Childcare Settings Safeguarding Children Risk Assessment Workbook for Childcare Settings
         <td colspan="2"> <?php print_error('courses',$errors)?></td>
      </tr>

       <tr>      <td colspan="2">&nbsp;</td></tr>
*Required Fields
        </tr>
      </table></form>[/code]

I have managed to get the email to send, however i can’t get it to work with the error message if somebody doesn’t select one of the checkboxes.

the html for one of the options is:

<input type='checkbox' name='courses[]' value='Health & Safety Essentials – Introduction to Working Safely'/>

The email php code is:

[php]foreach($_POST[‘courses’] as $courses)
{
$message.= “\n”."Courses: ".$courses;
}
[/php]

And the error php code in case someone does not check a box is (this is the bit the doesn’t work):

[php] if(!(isset($_POST[‘courses’]) && (strlen($_POST[‘courses’]) > 3))){

	$errors['courses'] = '<span style="color:red; font-size:12px">*Please select a course</span>';

	}

[/php]

The error message did show before I added the new code so not sure what I am missing…

If the $_POST[‘courses’] is submitted will be an array, then the strlen function won’t work.

You could replace this with the count function:

[php]if(!isset($_POST[‘courses’]) || count($_POST[‘courses’]) > 3)) {
// Error
}[/php]

Hi,

I have tried this and now the page comes up with he website cannot display the page

There was a ( missing! Thanks jSherz much appreciated!

Sponsor our Newsletter | Privacy Policy | Terms of Service