views:

105

answers:

4

An easy question:

How do I check in PHP if a checkbox is checked or not?

+7  A: 

If the checkbox is checked you the value of it will be passed. Otherwise it won't.

if (isset($_POST['mycheckbox'])) {
    echo "checked!";
}
Tomas Markauskas
+3  A: 

Try this

<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>
<?php
    if(isset($_POST['like'])
    {
        echo "You like Stackoverflow.";
    }
    else
    {
        echo "You don't like Stackoverflow.";
    }   
?>

Or this

<?php
    if(isset($_POST['like']) && 
    $_POST['like'] == 'Yes') 
    {
        echo "You like Stackoverflow.";
    }
    else
    {
        echo "You don't like Stackoverflow.";
    }   
?>
Michael B.
+2  A: 

http://www.html-form-guide.com/php-form/php-form-checkbox.html

This covers checkboxes, and checkbox groups.

McAden
A: 

I did a blog post on determining if a checkbox is checked or not, in the case you want the value from either case to be in your $_POST array.

Check out http://www.martinbean.co.uk/web-development/the-check-box-input-element/ for a more in-depth explanation.

Martin Bean