51

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

1

5 Answers 5

74

If the checkbox is checked, then the checkbox's value will be passed. Otherwise, the field is not passed in the HTTP post.

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

3 Comments

Does this work if a checkbox was checked, then unchecked and then the form was submitted?
Yes, the browser only sends the state that the checkbox was in, when the user clicked the submit button, how many times the checkbox was checked or unchecked doesn't matter.
Thank you. ASP.NET MVC handles it different, so i though that it would be similar in PHP, but looks like it's not. :)
31

you can check that by either isset() or empty() (its check explicit isset) weather check box is checked or not

for example

  <input type='checkbox' name='Mary' value='2' id='checkbox' />

here you can check by

if (isset($_POST['Mary'])) {
    echo "checked!";
}

or

if (!empty($_POST['Mary'])) {
    echo "checked!";
}

the above will check only one if you want to do for many than you can make an array instead writing separate for all checkbox try like

<input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br />
<input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br />
<input type="checkbox" name="formDoor[]" value="C" />Carnegie Complex<br />

php

  $aDoor = $_POST['formDoor'];
  if(empty($aDoor))
  {
    echo("You didn't select any buildings.");
  }
  else
  {
    $N = count($aDoor);
    echo("You selected $N door(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo htmlspecialchars($aDoor[$i] ). " ";
    }
  }

4 Comments

How to make it only be able to check one checkbox from multiple checkbox and insert it into database? insert into table_name (choice) VALUES ('$aDoor')
@AlKush use radio box instead ;)
Thanks. And yes I have decided to use radio buttons. But how to make it required. For instance. I have three radio buttons which has not been checked. But I want to remind the user that they have to choose one of them.
just check weather it is set or not with isset ex. if(isset($_POST['radio_buttonname']))
9

Try this

index.html

<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>

form.php

<html>
<head>
</head>
<body>

<?php
    if(isset($_POST['like']))
    {
        echo "<h1>You like Stackoverflow.<h1>";
    }
    else
    {
        echo "<h1>You don't like Stackoverflow.</h1>";
    }   
?>

</body>
</html>

Or this

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

Comments

7

If you don't know which checkboxes your page has (ex: if you are creating them dynamically) you can simply put a hidden field with the same name and 0 value right above the checkbox.

<input type="hidden" name="foo" value="0" />
<input type="checkbox" name="foo" value="1">

This way you will get 1 or 0 based on whether the checkbox is selected or not.

Comments

1

I love short hands so:

$isChecked = isset($_POST['myCheckbox']) ? "yes" : "no";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.