In our previous tutorials, we have performed various operations on different form elements using Javascript and jQuery. In this tutorial, our concern is to get values of multiple checked checkboxes using PHP as follows:
To get value of a checked checkbox :
<form action="#" method="post">
<input type="checkbox" name="gender" value="Male">Male</input>
<input type="checkbox" name="gender" value="Female">Female</input>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if (isset($_POST['gender'])){
echo $_POST['gender']; // Displays value of checked checkbox.
}
?>
To get value of multiple checked checkboxes, name attribute in HTML input type=”checkbox” tag must be initialize with an array, to do this write [ ] at the end of it’s name attribute :
<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
}
}
?>
In our example, there is a form contains some checkboxes, User checks them and when he/she hits submit button, multiple values of checkboxes will be display.
Watch our live demo or download our codes to use it.
Our example’s complete HTML and PHP codes are given below.
HTML Codes: php_checkbox.php
Given below our complete HTML codes.
<!DOCTYPE html>
<html>
<head>
<title>PHP: Get Values of Multiple Checked Checkboxes</title>
<link rel="stylesheet" href="css/php_checkbox.css" />
</head>
<body>
<div class="container">
<div class="main">
<h2>PHP: Get Values of Multiple Checked Checkboxes</h2>
<form action="php_checkbox.php" method="post">
<label class="heading">Select Your Technical Exposure:</label>
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label>
<input type="checkbox" name="check_list[]" value="HTML/CSS"><label>HTML/CSS</label>
<input type="checkbox" name="check_list[]" value="UNIX/LINUX"><label>UNIX/LINUX</label>
<input type="submit" name="submit" Value="Submit"/>
<!----- Including PHP Script ----->
<?php include 'checkbox_value.php';?>
</form>
</div>
</div>
</body>
</html>
PHP Codes: checkbox_value.php
In the below script, we used foreach loop to display individual value of checked checkboxes, we have also used a counter to count number of checked checkboxes.
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
}
echo "<br/><b>Note :</b> <span>Similarily, You Can Also Perform CRUD Operations using These Selected Values.</span>";
}
else{
echo "<b>Please Select Atleast One Option.</b>";
}
}
?>
CSS File: php_checkbox.css
Styling HTML elements.
/* Below line is used for online Google font */
@import url(http://fonts.googleapis.com/css?family=Droid+Serif);
div.container{
width: 960px;
height: 610px;
margin:50px auto;
font-family: 'Droid Serif', serif;
}
div.main{
width: 308px;
margin-top: 35px;
float:left;
border-radius: 5px;
Border:2px solid #999900;
padding:0px 50px 20px;
}
p{
margin-top: 5px;
margin-bottom: 5px;
color:green;
font-weight: bold;
}
h2{
background-color: #FEFFED;
padding: 25px;
margin: 0 -50px;
text-align: center;
border-radius: 5px 5px 0 0;
}
hr{
margin: 0 -50px;
border: 0;
border-bottom: 1px solid #ccc;
margin-bottom:25px;
}
span{
font-size:13.5px;
}
label{
color: #464646;
text-shadow: 0 1px 0 #fff;
font-size: 14px;
font-weight: bold;
}
.heading{
font-size: 17px;
}
b{
color:red;
}
input[type=checkbox]{
margin-bottom:10px;
margin-right: 10px;
}
input[type=submit]{
padding: 10px;
text-align: center;
font-size: 18px;
background: linear-gradient(#ffbc00 5%, #ffdd7f 100%);
border: 2px solid #e5a900;
color: #ffffff;
font-weight: bold;
cursor: pointer;
text-shadow: 0px 1px 0px #13506D;
width: 100%;
border-radius: 5px;
margin-bottom: 15px;
}
input[type=submit]:hover{
background: linear-gradient(#ffdd7f 5%, #ffbc00 100%);
}
Conclusion:
Once you got the value of checked checkbox(es), you can also perform CRUD (Create, Read, Update & Delete) operations in database. Hope you like it, keep reading our other blogs.
32 Replies to “PHP: Get Values of Multiple Checked Checkboxes”
thanks for the code…
I updated the value in the database and now i want to update and delete those values in the database.
please help me on it.
Thanx alot guys, your code really helped me. What if i want to display the selected items on another page?
Thanxs a lot , its good for learning and array are very useful for when multiple records are insert…
Hi! I want to know how can i put Value=”$_POST[‘My-option’]” on a selected checkbox, because if i make click on submit it show me you are selected 2 options, but the option disappear.
NIce, Simple, self-explanatory, thanks
Thanks for looking into it ..!
Keep reading our other blog posts :
https://www.formget.com/blog/
Its very help full thanq very much
Hello Priyanka,
That sounds really great ! Keep reading our other blog posts for getting more coding tricks.
Catch up new posts from here
Regards,
FormGet Team
Similar question as Thobile, September 9, 2014 at 7:07 am
I’m a newbee and have downloaded and modified your code. I have a webpage that offers 12-services (checkbox) and your “form” works fine and displays the name of the checkbox that was selected.
I’d like to take the variable(s) selected to a new page and perform a mysql insert with some additional form data from that is on a new page. You’ve said the variables are “CRUD” but how do I get the selected checkbox info to a new page without losing the current value(s)?
Do I do an echo somehow, on the new page, to know what was selected and then the mysql insert?
Thanks for the advice on this site. I am a beginner and I am working on a form that submits information into an email. The form is complete and everything is submitted to the email except the multiple checkbox selections. Initially it was just submitting 1 of the selected checkboxes. I added the [] to the check box names in the form and now I am getting a response of ‘array’ in the emailed results, so I believe its recognizing multiple selections but is just not listing them.
I know I probably need to add more script to my PHP page that process the form so that it lists all of the selections, but I’m not sure what to add and where. Any advice would help me move closer to my goal. Thanks. And I am definitely bookmarking this site.
Just what i needed. Wish i’d known about this site earlier. Thank You!
Thank you
for the code…
Thank you for the coding. It helped me a lot.
I deal with a various amount of checkboxes. It could be 10 but also 20.
So I used also a button “check all” and “uncheck all”.
Unfortunatly is doesn’t work.
Please can you help me with this problem?
Use ajax
thanks for the code ..
Thanks for the code.
this is very helpful.
Thanks for the code..
perfectly described
thanks a lot.
I don’t have a database, I want to send the values selected to the mail. How should I do it?
IT is right code for to get multiple checkbox value in php.
this code work for me………
Thanks
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): “;
// Loop to store and display values of individual checked checkbox.
foreach($_POST[‘check_list’] as $selected) {
echo “”.$selected .””;
}
echo “Note : Similarily, You Can Also Perform CRUD Operations using These Selected Values.”;
}
else{
echo “Please Select Atleast One Option.“;
}
}
?>
After using above code to select multiple options, may u help me a php script which will be used to send them into database?
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): “;
// Loop to store and display values of individual checked checkbox.
foreach($_POST[‘check_list’] as $selected) {
echo “”.$selected .””;
}
echo “Note : Similarily, You Can Also Perform CRUD Operations using These Selected Values.”;
}
else{
echo “Please Select Atleast One Option.“;
}
}
?>
We had use above code to select multiple options, so can help me another code which will be used to insert that selected options into database by using loop
How can I attach the checkboxes values to a mailing group data?
Many thanks for your response.
Johnny
Hi There Thanks a lot for this awesome code .
HI,
I have installed Chronoform v5 component to create a form for my Joomla site. For example My Checkbox group e.g, has following values:
Review: Good
Bad
Average
Suppose if any user Checks two items i.e, Bad and Average, the format in which it should save in database should be like: label:mesaage as Yes(if checked) or -(if not checked)
Review: Good: –
Bad: Yes
Average: Yes
In short it should display all checkbox values but should display ‘Yes’ in front of the value selected by user. Is it possible?? Any suggestions?
Thanks so much. This is just what I needed.
Thanks a lot, this is very useful.
This is a dynamic checkbox but we dont get chkid value on clicking delete button whereas we get value in sub
foreach($stuffs as $value)
{
if(isset($jobid[$job]))
{
$logged_user=$_SERVER[‘PHP_AUTH_USER’];
if($username[$job]==$logged_user)
{
$jobaction=”;
}
else{
$jobaction=”;
}
}
}
echo ”;
echo ”;
echo ”;
$checkbox_id=””;
if(isset($_POST[‘sub’]))
{
if(!empty($_POST[‘chkid’]))
{
foreach($_POST[‘chkid’] as $checkbox_id)
echo $checkbox_id;
}
}
Just want to commend your wonderful solutions, it’s indeed helpful.
sir i am not getting checked box details to email except that i am getting all informations .
here is the html code
Riyadh – 14th to 16th of February 2016
Dubai – 16th to 18th February 2016
Jeddah – 21st to 23rd of February 2016
and below is the php code
<?php
while (list ($key,$val) = @each ($checkboxone)) {
echo "$val,";
}
if (!empty($_POST)){
$to = "[email protected],[email protected]";
$fname = $_POST["fname"];
$company = $_POST["company"];
$jobtitle = $_POST["jobtitle"];
$email = $_POST["email"];
$mobilenumber = $_POST["mobilenumber"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$projects = $_POST["projects"];
$headers = 'Full name : ' . $fname . "rn" . ' Email id : ' . $email;
$message = wordwrap($message, 70, "rn");
$checkboxone = Trim(stripslashes($_POST['checkboxone']));
if(($fname != "Full Name" || $email != "Email Address") && ( $fname != "" && $email != "" ))
{
$message = 'Full Name: ' .$fname. "rn" .'Company Name: ' .$company. "rn" .'Job Title: ' .$jobtitle. "rn".'Email: ' .$email. "rn" .'Mobile Number: ' .$mobilenumber. "rn".'events: ' .$checkboxone. "rn";
mail($to, $subject, $message,$headers,$checkboxone);
echo '’;
echo ‘alert(“Message Sent Succesfully !!”)’;
echo ”;
}
}
?>
nice tutorial dude. but how to make input form with multiple checkbox ?
Hello,
Can you give me example similiar to this? But using MySQL to retrieve checked checkbox value and ajax to display selected values.
Thank you