An easy question:
How do I check in PHP if a checkbox is checked or not?
An easy question:
How do I check in PHP if a checkbox is checked or not?
If the checkbox is checked you the value of it will be passed. Otherwise it won't.
if (isset($_POST['mycheckbox'])) {
echo "checked!";
}
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.";
}
?>
http://www.html-form-guide.com/php-form/php-form-checkbox.html
This covers checkboxes, and checkbox groups.
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.