简体   繁体   中英

How to check Checkbox not checked in foreach

I have html checkbox like this:

<form action="" method="post">
     <input type="checkbox" name="language[]" value="php" />PHP<br />
     <input type="checkbox" name="language[]" value="html" />HTML<br />
     <input type="checkbox" name="language[]" value="java" />Java<br />
     <input type="checkbox" name="language[]" value="c++" />C++<br />
     <input type="submit" value="send" />
</form>

Now I want to detect the checkbox is not checked using this PHP

if($_POST)
{
     if(empty($_POST['language']))
     {
         echo "bla";
     }
     else
     {
         foreach($_POST['language'] as $value)
         {
             echo 'Checked: '.$value.'
         ';
         }
     }
}

The output is always show the checbox checked. My question is, how can I detect the checkbox is not checked? Example I do not check PHP and Java.

You don't need to validate checkbox by checkbox in order to determine if they are checked or not, you won't get the unchecked checkboxes values at the time you send the form, so, sending the form like this:

<form action="" method="post">
     <input type="checkbox" name="language[]" value="php" />PHP<br /> <!-- checked -->
     <input type="checkbox" name="language[]" value="html" />HTML<br /><!-- checked -->
     <input type="checkbox" name="language[]" value="java" />Java<br /><!-- unchecked -->
     <input type="checkbox" name="language[]" value="c++" />C++<br /><!-- unchecked -->
     <input type="submit" value="send" />
</form>

In your PHP, you will get an array as follows:

$_POST['languages'] = array("php", "html");

Now, lets say you have an array of all the values in order to check which ones you need to delete, and which ones you need to add, a rough code example would be as follows:

$allValues = array('php', 'html', 'java', 'c++');
$valuesForAdd = $_POST['language'];
$valuesForDeletion = array_diff($allValues, $valuesForAdd);

Only 'checked' checkboxes get sent as parameters in a POST request.

If you want to know which aren't checked, you could have the value list stored on PHP side; then once you receive POST data - compare the array on PHP side with POST array.

$all_vals = array('php', 'c++', 'html', 'java');
$post_vals = $_POST['languages'];  

foreach ($post_vals as $post_val)
  if in_array($post_val, $all_vals)
     $checkbox checked
  else
     $checkbox not checked

I assume this gives you enough liberty to do what you need.

First you need the selectable items array in the backend:

$items = array('php','html','java','c++');

You have the posted (selected) languages array here:

$_POST['language']

Not selected languages array:

$not_selected_languages = array_diff($items,$_POST['language']);

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM