How do I see which checkbox is checked? How do I see which checkbox is checked? php php

How do I see which checkbox is checked?


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!";}


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] ). " ";    }  }


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.";    }   ?>