Computing.Net > Forums > Programming > Sending Checkbox Values in PHP

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Sending Checkbox Values in PHP

Reply to Message Icon

Name: ksdicke2
Date: February 28, 2009 at 13:36:13 Pacific
OS: Macintosh
Subcategory: General
Comment:

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>




Sponsored Link
Ads by Google

Response Number 1
Name: shutat
Date: February 28, 2009 at 23:17:55 Pacific
Reply:

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
}
?>


0

Response Number 2
Name: ksdicke2
Date: March 1, 2009 at 11:36:19 Pacific
Reply:

Where in the code do I put that. Also what is the "cmd" Do i delete
the

$order = $_POST['order'];

that I already had?



0

Response Number 3
Name: ksdicke2
Date: March 1, 2009 at 11:42:31 Pacific
Reply:

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.



0

Response Number 4
Name: shutat
Date: March 1, 2009 at 15:55:23 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


regarding error Photoshop error



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Sending Checkbox Values in PHP

problem with date() function in PHP www.computing.net/answers/programming/problem-with-date-function-in-php/8855.html

Any help is GOOD.Sockets in PHP/win www.computing.net/answers/programming/any-help-is-goodsockets-in-phpwin/5368.html

sending email in php www.computing.net/answers/programming/sending-email-in-php/17961.html