How do I see which checkbox is checked?
Asked Answered
S

5

49

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

Shortsighted answered 15/2, 2010 at 21:4 Comment(1)
http://www.html-form-guide.com/php-form/php-form-checkbox.html This covers checkboxes, and checkbox groups.Disoblige
S
73

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!";
}
Selby answered 15/2, 2010 at 21:6 Comment(3)
Does this work if a checkbox was checked, then unchecked and then the form was submitted?Frock
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.Selby
Thank you. ASP.NET MVC handles it different, so i though that it would be similar in PHP, but looks like it's not. :)Frock
Y
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] ). " ";
    }
  }
Ynez answered 4/12, 2012 at 18:26 Comment(4)
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')Mavilia
@AlKush use radio box instead ;)Sunrise
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.Mavilia
just check weather it is set or not with isset ex. if(isset($_POST['radio_buttonname']))Sunrise
S
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.";
    }   
?>
Schwitzer answered 15/2, 2010 at 21:6 Comment(0)
P
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.

Plasmolysis answered 24/7, 2015 at 4:2 Comment(0)
E
1

I love short hands so:

$isChecked = isset($_POST['myCheckbox']) ? "yes" : "no";
Enrage answered 17/7, 2017 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.