Gero
Gero

Reputation: 47

How to get unselected checkbox?

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 the unselected items also, like this: 0=yes,1=no,3=yes and so on.

Can I solve this somehow?

Upvotes: 1

Views: 1018

Answers (4)

Carlos Lima
Carlos Lima

Reputation: 4182

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], ..
?>

Upvotes: 0

BalusC
BalusC

Reputation: 1109262

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.

Upvotes: 1

asgerhallas
asgerhallas

Reputation: 17724

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

Upvotes: 3

Gumbo
Gumbo

Reputation: 655519

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.

Upvotes: 1

Related Questions