Solved

Multiple Checkboxes in PHP Form Submission

Posted on 2010-09-17
7
497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
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…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

749 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