How do you checkbox is checked or not in php?

Updated on July 1, 2020

by Neeraj Agarwal

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 :


Male
Female


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 :


C/C++
Java
PHP

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.




PHP: Get Values of Multiple Checked Checkboxes



PHP: Get Values of Multiple Checked Checkboxes

Select Your Technical Exposure: C/C++ Java PHP HTML/CSS UNIX/LINUX

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.

CSS File: php_checkbox.css

Styling HTML elements.

/* Below line is used for online Google font */
@import url[//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”

  1. 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.

    Reply

  2. Thanx alot guys, your code really helped me. What if i want to display the selected items on another page?

    Reply

  3. Thanxs a lot , its good for learning and array are very useful for when multiple records are insert…

    Reply

  4. 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.

    Reply

  5. NIce, Simple, self-explanatory, thanks

    Reply

    1. Reply

  6. Its very help full thanq very much

    Reply

    1. 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

      Reply

  7. 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?

    Reply

  8. 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.

    Reply

  9. Just what i needed. Wish i’d known about this site earlier. Thank You!

    Reply

  10. Reply

  11. 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?

    Reply

    1. Use ajax

      Reply

  12. thanks for the code ..

    Reply

  13. Thanks for the code.
    this is very helpful.

    Reply

  14. Thanks for the code..

    Reply

  15. perfectly described
    thanks a lot.

    Reply

  16. I don’t have a database, I want to send the values selected to the mail. How should I do it?

    Reply

  17. IT is right code for to get multiple checkbox value in php.
    this code work for me………

    Thanks

    Reply

Bài mới nhất

Chủ Đề