[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Multiple Checkboxes in PHP Form Submission

Posted on 2010-09-17
7
Medium Priority
?
530 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 2000 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 44

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 44

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
To show how to generate a certificate request 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 Servers >> Certificates…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

656 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