未选中复选框时发布复选框值[重复]

This question already has an answer here:

How to POST checkbox to off for example when it's not checked as checboxes submit values only when it's checked and I don't want to use hidden variables

Thanks

</div>

The short answer is the browser doesn't send unchecked checkboxes. But one possible work around is to on the php side set a defaults array

So for a form that included fields like

<input type='checkbox' name='checkbox[a]' value='1'/>
<input type='checkbox' name='checkbox[b]' value='1'/>
<input type='checkbox' name='checkbox[c]' value='1'/>

you would say

$checkbox_defaults = array(
   "a" => 0,
   "b" => 0,
   "c" => 0
);

Then on PHP say

$_POST["checkbox"] = array_merge($checkbox_defaults, $_POST["checkbox"]);

NOTE this only works for string indexed arrays ... if you need to work with numerically indexed arrays the php should look like this.

$checkbox_defaults = array(
   0 => 0,
   1 => 0,
   2 => 0
);
foreach($checkbox_defaults as $k=>$v){
    $_POST["checkbox"][$k] = (isset($_POST["checkbox"][$k])?
                             $_POST["checkbox"][$k]:$v);
}