Hey I just wanted to know how do we check if a user has checked a checkbox or not in a form after hitting the submit buttom . I am using PHP so any help regarding that would be really appreciated . Thanks
Asked
Active
Viewed 4.6k times
-4
-
1Can you show us your effort? – Mohit Mehta Mar 20 '13 at 10:08
-
can you post your checkbox code here? – Dead Man Mar 20 '13 at 10:08
-
– Abhinav Mar 20 '13 at 10:15
-
do some basic search before asking to stack.. – Shiju Shaji Mar 20 '13 at 11:03
3 Answers
5
You can do it like this:
if(isset($_POST['checkbox_name']) && $_POST['checkbox_name']!="")
{
echo 'checkbox is checked';
}

Dead Man
- 2,880
- 23
- 37
-
-
Hey I have a little more doubt now , whats the difference between using a 'name' and 'value' in an input checkbox ? – Abhinav Mar 20 '13 at 10:13
-
`name` and `value` are totally different `attributes` of a checkbox. `name` is used to fetch the value of element, and `value` is the value of that element. – Dead Man Mar 20 '13 at 10:15
3
<input type='checkbox' name='boxname' value='1' />
<?php
if(isset($_POST['boxname'])
{
echo "check box is checked";
}
?>

Mohit Mehta
- 1,283
- 12
- 21
1
HTML
<input type='checkbox' name='theName' value='itsChecked'>
PHP
if($_POST['theName'] == 'itsChecked'){}

advermark
- 231
- 1
- 10
-
3
-
Good point.. Input needs name='theName' & If statement will be $_POST['theName'] instead. – advermark Mar 20 '13 at 10:13
-
1It's better to use `isset` first and then compare `==` to avoid warnings – asprin Mar 20 '13 at 10:15