shehzad lakhani shehzad lakhani - 1 year ago 100
PHP Question

how to get the value of multiple unselected checkboxes in php?

i tried for selected chcekboxes but

<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
<input type="submit" name="submit" value="Submit"/>

if(isset($_POST['submit'])){//to run PHP script on submit
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";

how to get the value for unselected checkboxes?

Answer Source

Please note that your unselected checkboxes won't be available in your POST data. Your POST will contain only the ones which are checked.

You can store all possible options in the $options, and all the checked options in $checked.

We can find the unchecked options by using array_diff i.e by finding the difference between $options and $selected.

if (isset($_POST['submit'])) { //to run PHP script on submit
    $options = array("C/C++", "Java", "PHP");
    $checked = isset($_POST['check_list']) ? $_POST['check_list'] : array();
    $unchecked = array_diff($options, $checked);
    /* Other code */
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download