-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

4
  • 1
    Can you show us your effort? Commented Mar 20, 2013 at 10:08
  • can you post your checkbox code here? Commented Mar 20, 2013 at 10:08
  • <input name="agree" type="checkbox" value="Check" /> Commented Mar 20, 2013 at 10:15
  • do some basic search before asking to stack.. Commented Mar 20, 2013 at 11:03

3 Answers 3

5

You can do it like this:

if(isset($_POST['checkbox_name']) && $_POST['checkbox_name']!="")
{
    echo 'checkbox is checked'; 
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks I was using 'name' of the checkbox instead the 'value' .
Hey I have a little more doubt now , whats the difference between using a 'name' and 'value' in an input checkbox ?
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.
3
<input type='checkbox' name='boxname' value='1' />

<?php
    if(isset($_POST['boxname'])
    {
        echo "check box is checked";
    }
?>

Comments

1

HTML

<input type='checkbox' name='theName' value='itsChecked'>

PHP

if($_POST['theName'] == 'itsChecked'){}

3 Comments

Where's the name attribute?
Good point.. Input needs name='theName' & If statement will be $_POST['theName'] instead.
It's better to use isset first and then compare == to avoid warnings

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.