Solved

Multiple Checkboxes in PHP Form Submission

Posted on 2010-09-17
7
486 Views
Last Modified: 2013-12-17
I have multiple checkboxes in a form that need to be sent when submitted.  Right now when I submit the form the value I get for my checkbox group is "Array".  I know I need to convert to a string, I'm just unfamiliar with PHP.  Any help would be appreciated - code is posted below.
<?php
if(isset($_POST['Submit'])) {

$cname=$_POST['cname'];
$cnumber=$_POST['cnumber'];
$btime=$_POST['btime'];
$group1=$_POST['group1'];
$group2=$_POST['group2'];
$caddress=$_POST['caddress'];
$agent=$_POST['agent'];


$to="me@email.com";
$subject="Form Submission";
$from="email.com";
$data="Submission Form"."\n"
		."Customer Name : ".$cname."\n"
		."Customer Number : ".$cnumber."\n"
		."Best Time : ".$btime."\n"
		."AM or PM : ".$group1."\n"
		."Customer Address : ".$caddress."\n"
		."Agent : ".$agent."\n"
		."Insurance Type : ".$group2."\n";
		
mail($to,$subject,$data,"FROM : $from");
}
?>

Open in new window

<label>Homeowners Insurance </label><input name="group2[]" type="checkbox" value="Homeowners Insurance">&nbsp;&nbsp;&nbsp;<label>Auto Insurance </label><input name="group2[]" type="checkbox" value="Auto Insurance">&nbsp;&nbsp;&nbsp;<label>Life Insurance </label><input name="group2[]" type="checkbox" value="Life Insurance">&nbsp;&nbsp;&nbsp;<label>Commercial Insurance </label><input name="group2[]" type="checkbox" value="Commericial Insurance">

Open in new window

0
Comment
Question by:intear
  • 3
  • 2
  • 2
7 Comments
 
LVL 26

Expert Comment

by:jar3817
ID: 33702212
You just access each checkbox in the array to see if it's value is "On" if so, it's checked

$_POST['arrayname'][0] through $_POST['arrayname'][n]
0
 

Author Comment

by:intear
ID: 33702482
I tried the following and got the same "Array" output.
<?php
if(isset($_POST['Submit'])) {

$cname=$_POST['cname'];
$cnumber=$_POST['cnumber'];
$btime=$_POST['btime'];
$group1=$_POST['group1'];
$group2=$_POST['group2'];
$caddress=$_POST['caddress'];
$agent=$_POST['agent'];

if (isset($_POST['group2'][0])){
echo "Homeowners Insurance&nbsp;";
}
elseif (isset($_POST['group2'][1])){
echo "Auto Insurance&nbsp;";
}
elseif (isset($_POST['group2'][2])){
echo "Life Insurance&nbsp;";
}
elseif (isset($_POST['group2'][3])){
echo "Commericial Insurance&nbsp;";
}
else{
echo "Please Select Insurance Type<br />";
}


$to="troyer@intear.com";
$subject="First Advantage Form Submission";
$from="erafirst.com";
$data="Submission Form"."\n"
		."Customer Name : ".$cname."\n"
		."Customer Number : ".$cnumber."\n"
		."Best Time : ".$btime."\n"
		."AM or PM : ".$group1."\n"
		."Customer Address : ".$caddress."\n"
		."Agent : ".$agent."\n"
		."Insurance Type : ".$group2."\n";
		
mail($to,$subject,$data,"FROM : $from");
}
?>

Open in new window

0
 
LVL 26

Accepted Solution

by:
jar3817 earned 500 total points
ID: 33702573
In line 8 you're setting $group2 equal to the array. Therefore on line 39, you'll need to access the array elements like $group2[0] ...not just $group2
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 33702651
Hi intear,

With checkboxes, a value only appears in the array if it is ticked. If left unticked it won't be in the array. You should loop through the array, and build a string based on the ticked values.

Replace your line 8 with the following:

$group2 = "";
foreach ($_POST['group2'] as $key => $value) {
    $group2 .= $value . " | ";
}

Open in new window

0
 

Author Comment

by:intear
ID: 33702658
I changed line 39 to this ( ."Insurance Type : ".$group2[0]."\n".$group2[1]."\n".$group2[2]."\n".$group2[3]."\n"; ) and it worked great.

Thanks for your help.
0
 
LVL 26

Expert Comment

by:jar3817
ID: 33702670
Glad it's working for you!
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 33702759
If you add checkboxes to your form you'll need to edit your code - looping through the array is more robust and won't need changing. Imagine trying to run your code if you have 30 checkboxes !! Nightmare :(
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now