Sending Checkbox Values in PHP

February 28, 2009 at 13:36:13
Specs: Macintosh
I have a simple form that sends me an email with all the
information that the user inputs. I have a list with
checkboxes. The email that I get does not display which
checkboxes the person choose. It just says "Array". Can
someone help?

This is the php

$company = $_POST['company'];
	$address = $_POST['address'];
	$city = $_POST['city'];
	$contact = $_POST['contact'];
	$phone = $_POST['phone'];
	$email = $_POST['email'];
	$website = $_POST['website'];
	$employees = $_POST['employees'];
	$order = $_POST['order'];

and this is the html checkboxes


<label>
<input type="checkbox" name="order[0]" 
value="Recruiting Assistance (electronic job boards, 
resume review, legal interviews, skill testing, final 
selection process)" id="order" /> 
Recruiting Assistance (electronic job boards, resume 
review, legal interviews, skill testing, final selection 
process)</label>
		      
		      <br />
		      <label>
<input type="checkbox" name="order[1]" value="Pre-
employment Process (background checks, drug testing, 
employment and education verifications)" id="order_1" /> 
Pre-employment Process (background checks, drug 
testing, employment and education verifications)</label>
		      
			  <br />
		      <label>
		      <input type="checkbox" name="order[2]" 
value="Employment Forms (applications, benefits, tax 
forms)" id="order_2" />
Employment Forms (applications, benefits, tax 
forms)</label>
		      <br />
		      <label>
		      <input type="checkbox" name="order[3]" 
value="South Carolina Illegal Immigration Reform Act 
Process" id="order_3" />
South Carolina Illegal Immigration Reform Act 
Process</label>
		      <br />
		      <label>
		      <input type="checkbox" name="order[4]" 
value="Policies and Procedures" id="order_4" />
Policies and Procedures</label>
		      <br />
		      <label>
		      <input type="checkbox" name="order[5]" 
value="Employee Handbook" id="order_5" />
Employee Handbook</label>
		      <br />
		      <label></label>
		     
		      <label>
<input type="checkbox" name="order[6]" value="Job 
Descriptions" id="order_6" />
Job Descriptions</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[7]" 
value="Employment Law Posters" id="order_7" />
Employment Law Posters</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[8]" 
value="Salary and Benefit Analysis" id="order_8" />
Salary and Benefit Analysis</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[9]" 
value="Employment Law Posters" id="order_9" />
Employment Law Posters</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[10]" 
value="Employee Recognition/Service Awards" 
id="order_10" />
Employee Recognition/Service Awards</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[11]" 
value="Employee Relations/Conflict 
Resolution/Coaching" id="order_11" />
Employee Relations/Conflict 
Resolution/Coaching</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[12]" 
value="Performance Reviews" id="order_12" />
Performance Reviews</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[13]" 
value="Performance Improvement Plans" id="order_13" />
Performance Improvement Plans</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[14]" 
value="Legal Separation Agreements" id="order_14" />
Legal Separation Agreements</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[15]" 
value="Downsizing/Out Placement Services" 
id="order_15" />
Downsizing/Out Placement Services</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[16]" 
value="Family Medical Leaves (FMLA)" id="order_16" />
Family Medical Leaves (FMLA)</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[17]" 
value="Assistance with Equal Employment Opportunity 
Commission (EEOC) Claims" id="order_17" />
Assistance with Equal Employment Opportunity 
Commission (EEOC) Claims</label>
			<br />
		      <label>
<input type="checkbox" name="order[18]" 
value="Affirmative Action Plan (AAP)" id="order_18" /> 
Affirmative Action Plan (AAP)</label></p>

	          <br/><input name="submit" type="submit" 
id="submit" value="Submit "/>
	          <input type="reset" name="clear" id="clear" 
value="Reset "/>
	      </form>



See More: Sending Checkbox Values in PHP

Report •


#1
February 28, 2009 at 23:17:55
Maybe the code below will help.

<?php
   if(isset($_POST["cmd"])) {
      // other form vars
      $order = null;

       for($i=0; $i < count($_POST["chk"]); $i++) { 
          $order .= $_POST["chk"][$i] . "\r\n";
       }

       echo (is_null($order) === false ? $order : "no selections made");
 
   } else {
?>

<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
check1 <input name="chk[]" value='choice 1' type="checkbox">
check2 <input name="chk[]" value='choice 2' type="checkbox">
check3 <input name="chk[]" value='choice 3' type="checkbox">
check4 <input name="chk[]" value='choice 4' type="checkbox">
<input name="cmd" value="Send" type="submit">
</form>
<?php
}
?>


Report •

#2
March 1, 2009 at 11:36:19
Where in the code do I put that. Also what is the "cmd" Do i delete
the
$order = $_POST['order'];

that I already had?



Report •

#3
March 1, 2009 at 11:42:31
Okay, I changed the code to this....
/* Gathering Data Variables*/

	$company = $_POST['company'];
	$address = $_POST['address'];
	$city = $_POST['city'];
	$contact = $_POST['contact'];
	$phone = $_POST['phone'];
	$email = $_POST['email'];
	$website = $_POST['website'];
	$employees = $_POST['employees'];
	
	
	if(isset($_POST["cmd"])) {
      // other form vars
      $order = null;

       for($i=0; $i < count($_POST["order"]); $i++) { 
          $order .= $_POST["chk"][$i] . "\r\n";
       }

       echo (is_null($order) === false ? $order : "no selections 
made");
 
   } else {
  
	
	
	$body = <<<EOD

<hr>

Company: $company 

Address: $address 

City: $city 

Contact Person: $contact 

Phone: $phone 

Email: $email 

website: $website 

Number of Employees: $employees 

Requested Services: $order 

EOD;

	$headers = "From: $email\r\n";
	$headers .= "Content-type: text/html\r\n";
	$success = mail($webMaster, $emailSubject, $body, 
$headers);

and in the html to this....

      
	            
	            

<label>
<input type="checkbox" name="order[]" value="Recruiting 
Assistance (electronic job boards, resume review, legal 
interviews, skill testing, final selection process)"  /> 
Recruiting Assistance (electronic job boards, resume review, 
legal interviews, skill testing, final selection process)</label>
		      
		      <br />
		      <label>
<input type="checkbox" name="order[]" value="Pre-employment 
Process (background checks, drug testing, employment and 
education verifications)" /> 
Pre-employment Process (background checks, drug testing, 
employment and education verifications)</label>
		      
			  <br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Employment Forms (applications, benefits, tax forms)"  />
Employment Forms (applications, benefits, tax forms)</label>
		      <br />
		      <label>
		      <input type="checkbox" name="order[]" value="South 
Carolina Illegal Immigration Reform Act Process"  />
South Carolina Illegal Immigration Reform Act Process</label>
		      <br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Policies and Procedures"  />
Policies and Procedures</label>
		      <br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Employee Handbook"  />
Employee Handbook</label>
		      <br />
		      <label></label>
		     
		      <label>
<input type="checkbox" name="order[]" value="Job Descriptions"  
/>
Job Descriptions</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Employment Law Posters"  />
Employment Law Posters</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Salary and Benefit Analysis"  />
Salary and Benefit Analysis</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Employment Law Posters"  />
Employment Law Posters</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Employee Recognition/Service Awards"  />
Employee Recognition/Service Awards</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Employee Relations/Conflict Resolution/Coaching" />
Employee Relations/Conflict Resolution/Coaching</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Performance Reviews"  />
Performance Reviews</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Performance Improvement Plans"  />
Performance Improvement Plans</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[]" value="Legal 
Separation Agreements"  />
Legal Separation Agreements</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Downsizing/Out Placement Services"  />
Downsizing/Out Placement Services</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Family Medical Leaves (FMLA)"  />
Family Medical Leaves (FMLA)</label>
			<br />
		      <label>
		      <input type="checkbox" name="order[]" 
value="Assistance with Equal Employment Opportunity 
Commission (EEOC) Claims"  />
Assistance with Equal Employment Opportunity Commission 
(EEOC) Claims</label>
			<br />
		      <label>
<input type="checkbox" name="order[]" value="Affirmative Action 
Plan (AAP)"  /> 
Affirmative Action Plan (AAP)</label></p>

	          <br/><input name="submit" type="submit" id="submit" 
value="Submit "/>
	          <input type="reset" name="clear" id="clear" 
value="Reset "/>
	      </form>
	    </div>

And now It does not work at all. I do not even get an email
anymore. Im sure im doing something wrong. Thanks for your
help so far.



Report •

Related Solutions

#4
March 1, 2009 at 15:55:23
I apologize if I've confused you - the code I posted was just a sample of how to parse an array. Keep everything you have previously with the execption of how you handled $order = $_POST['order'].

Since your form uses an array for the checkboxes, php will see $_POST["order"] as an array, but only set a value if a checkbox has been checked.

For example, if your form has 5 checkboxes, but a user only sets checkbox 1, 4, and 5, then php will return an array of three elements with [0] = checkbox 1, [1] = checkbox 4, and [2] = checkbox 5.

In your code, modify it where you process $order = $_POST['order'] .

$order = null;

for($i=0; $i < count($_POST["order"]); $i++) {
   $order .= $_POST["order"][$i] . "\r\n";
}

if(!is_null($order)) { 
   echo "order = " . $order;
} else {
   echo "order = no selection made";
}

I haven't checked the code above for syntax, but something like that *should* at least give you a result for $order in your email.

HTH


Report •


Ask Question