How do I check in PHP whether a checkbox
is checked or not?
Asked
Active
Viewed 1.7e+01k times
48

NullPoiиteя
- 56,591
- 22
- 125
- 143

Karem
- 17,615
- 72
- 178
- 278
-
[http://www.html-form-guide.com/php-form/php-form-checkbox.html](http://www.html-form-guide.com/php-form/php-form-checkbox.html) This covers checkboxes, and checkbox groups. – McAden Feb 15 '10 at 21:09
5 Answers
73
If the checkbox is checked, then the checkbox's value will be passed. Otherwise, the field is not passed in the HTTP post.
if (isset($_POST['mycheckbox'])) {
echo "checked!";
}

bryjohns
- 646
- 4
- 18

Tomas Markauskas
- 11,496
- 2
- 33
- 35
-
1Does this work if a `checkbox` was checked, then unchecked and then the form was submitted? – Jo Smo Aug 09 '15 at 12:53
-
2Yes, the browser only sends the state that the checkbox was in, when the user clicked the submit button, how many times the checkbox was checked or unchecked doesn't matter. – Tomas Markauskas Aug 09 '15 at 19:40
-
Thank you. `ASP.NET MVC` handles it different, so i though that it would be similar in `PHP`, but looks like it's not. :) – Jo Smo Aug 10 '15 at 05:35
30
you can check that by either isset()
or empty()
(its check explicit isset) weather check box is checked or not
for example
<input type='checkbox' name='Mary' value='2' id='checkbox' />
here you can check by
if (isset($_POST['Mary'])) {
echo "checked!";
}
or
if (!empty($_POST['Mary'])) {
echo "checked!";
}
the above will check only one if you want to do for many than you can make an array instead writing separate for all checkbox try like
<input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br />
<input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br />
<input type="checkbox" name="formDoor[]" value="C" />Carnegie Complex<br />
php
$aDoor = $_POST['formDoor'];
if(empty($aDoor))
{
echo("You didn't select any buildings.");
}
else
{
$N = count($aDoor);
echo("You selected $N door(s): ");
for($i=0; $i < $N; $i++)
{
echo htmlspecialchars($aDoor[$i] ). " ";
}
}

NullPoiиteя
- 56,591
- 22
- 125
- 143
-
How to make it only be able to check one checkbox from multiple checkbox and insert it into database? `insert into table_name (choice) VALUES ('$aDoor')` – Al Kush Nov 02 '14 at 10:28
-
-
Thanks. And yes I have decided to use radio buttons. But how to make it required. For instance. I have three radio buttons which has not been checked. But I want to remind the user that they have to choose one of them. – Al Kush Nov 02 '14 at 11:27
-
just check weather it is set or not with isset ex. if(isset($_POST['radio_buttonname'])) – NullPoiиteя Nov 02 '14 at 11:43
9
Try this
index.html
<form action="form.php" method="post">
Do you like stackoverflow?
<input type="checkbox" name="like" value="Yes" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
form.php
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['like']))
{
echo "<h1>You like Stackoverflow.<h1>";
}
else
{
echo "<h1>You don't like Stackoverflow.</h1>";
}
?>
</body>
</html>
Or this
<?php
if(isset($_POST['like'])) &&
$_POST['like'] == 'Yes')
{
echo "You like Stackoverflow.";
}
else
{
echo "You don't like Stackoverflow.";
}
?>

Community
- 1
- 1

Michael B.
- 3,410
- 1
- 21
- 32
7
If you don't know which checkboxes your page has (ex: if you are creating them dynamically) you can simply put a hidden field with the same name and 0 value right above the checkbox.
<input type="hidden" name="foo" value="0" />
<input type="checkbox" name="foo" value="1">
This way you will get 1 or 0 based on whether the checkbox is selected or not.

Can Celik
- 2,050
- 21
- 30