Solved

Multiple Checkboxes in PHP Form Submission

Posted on 2010-09-17
7
479 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 42

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 42

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
These days socially coordinated efforts have turned into a critical requirement for enterprises.
In this video we show how to create a Shared Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Sha…
In this video we show how to create an Address List in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Organization >> Ad…

743 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

13 Experts available now in Live!

Get 1:1 Help Now