-4

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

Abhinav
  • 35
  • 1
  • 2
  • 7

3 Answers3

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
  • thanks I was using 'name' of the checkbox instead the 'value' . – Abhinav Mar 20 '13 at 10:11
  • 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