开发者

How to get unselected checkbox?

开发者 https://www.devze.com 2022-12-10 16:36 出处:网络
I have three checkboxes like ch[0], ch[1] and ch[3] (sometimes i have more, or less, it\'s dinamic) and in PHP i want to get th开发者_如何学运维e unselected items also, like this: 0=yes,1=no,3=yes and

I have three checkboxes like ch[0], ch[1] and ch[3] (sometimes i have more, or less, it's dinamic) and in PHP i want to get th开发者_如何学运维e unselected items also, like this: 0=yes,1=no,3=yes and so on.

Can I solve this somehow?


A common way is to put a hidden form field beside the checkbox and then via javascript set the value for that when the checkbox is changed.

EDIT: You don't need javascript. But the hidden field is a way to go, when you don't neccessarily know, on the page that is posted to, how many checkboxes there are on the requesting page. Check out: http://www.felgall.com/xtutf06a.htm


Unselected checkboxes are not being submitted. So you can only determine the unselected checkboxes by determining the set of all available checkboxes minus the selected checkboxes.


Why do you need the unselected ones if you have both the complete list and the selected ones in the server side? Just extract the unselected ones from the complete list by filtering the selected ones out.


You can do this on HTML:

<input type="hidden" name="ch[0]" value="no">
<input type="checkbox" name="ch[0]" value="yes">
...
<input type="hidden" name="ch[5]" value="no">
<input type="checkbox" name="ch[5]" value="yes">

And check it the regular way on PHP:

<?php
  $ch = $_REQUEST['ch'];
  //then use $ch[0], $ch[1], ..
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消