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
Solved

Multiple Checkboxes in PHP Form Submission

Posted on 2010-09-17
7
491 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
Back Up Your Microsoft Windows Server®

Back up 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The new Gmail Phishing Scam going around is surprising even the savviest of users with its sophisticated techniques.
In this video we show how to create an Accepted Domain 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 Mail Flow >> Ac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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