How to check if a checkbox is checked with PHP?

Question:

How to check if a checkbox is checked in a form submit?

Answer:

If your HTML page looks like this:

<input type="checkbox" name="meucheckbox" value="umvalorqualquer">

When sent, if the check box is not checked, the variable meucheckbox will not exist, its value is NULL . If it is checked, it receives the variable meucheckbox and its value will be avalueany .

On the PHP page you can check as follows if the form is submitted via POST:

if(isset($_POST['meucheckbox']))
{
    echo "checkbox marcado! <br/>";
    echo "valor: " . $_POST['meucheckbox'];
}
else
{
    echo "checkbox não marcado! <br/>";
}

If the form is submitted via GET:

if(isset($_GET['meucheckbox']))
{
    echo "checkbox marcado! <br/>";
    echo "valor: " . $_GET['meucheckbox'];
}
else
{
    echo "checkbox não marcado! <br/>";
}

If the checkbox is checked, the result will be:

checkbox marcado!
valor: umvalorqualquer

If the checkbox is not checked, the result will be:

checkbox não marcado!
Scroll to Top