views:

716

answers:

7

I noticed that PHP seems to return only values of checked checkboxes. I would like to see a list of checkboxes, not just values of checked checkboxes. Is there a way to detect variables of unchecked boxes?

I asked because I want to be able to update settings. For example, I have a few options that are already checked but if an user decides to uncheck an option, I need to know that unchecked value so I can update the option to be disabled.

A: 

A pure PHP implementation doesn't seem possible, you can try using jQuery/AJAX though.

Alix Axel
A: 

This isn't something that can be done purely in PHP.

Browsers only send information about checkboxes if they are checked, if you want to also send information about unchecked checkboxes, you'll have to add hidden fields in the form and use javascript to manage them.

Ben S
+4  A: 

I just ran into this problem myself. I solved it by adding a duplicate hidden field with the same name. When the browser sends this information, the second field overrides the first (so ensure that the hidden field comes first).

<input type="hidden" name="foo" value="">
<input type="checkbox" name="foo" value="bar">

If the checkbox is not checked you get:

$_REQUEST[ 'foo' ] == ""

If the checkbox is checked you get:

$_REQUEST[ 'foo' ] == "bar"
Peter Kovacs
Wow! Awesome solution! Thanks everyone.
netrox
A really smart idea! +1
o.k.w
A: 

Suppose you have a 3 checkboxes you want to check, and update_settings is the name of your functions that take the checkbox name as a first argument and a bool value as a second one (activate or not).

Take the following snippet:

$checkboxes = array("checkbox1", "checkbox2", "checkbox3");
foreach($checkboxes as $checkbox){
    $checked = isset($_POST[$checkbox]);
    update_settings($checkbox, $checked);
}

Althouth Peter Kovacs solution it's going to work, I don't think it's practical since you can already check your variables using isset.

GmonC
+1  A: 

I see that the answer of Peter Kovacs is been accepted. I would only warn: be careful with this. Although this is in the most cases indeed intuitively from top to bottom, but the processing order of the request parameters is nowhere specified in some standard. So the behaviour may be webbrowser dependent. I really wouldn't let my application rely on some unspecified and external factors.

I would rather pass a separate array containing all values and just do the retaining/filtering work in the server side.

But this boils down to a new question: why do you need to know the unchecked checkboxes as well if you already know all checkboxes beforehand? Just do the math all - checked = unchecked.

BalusC
HTML 4.01 seems to indicate otherwise (http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4): "The control names/values are listed in the order they appear in the document."
Peter Kovacs
There are some scenarios when you DON'T know beforehand how many checkboxes you need. If you want to computer that it would require additional processing, which is not eveytime what you want. But your point is good. +1.
Timotei Dolean
+1  A: 

firefox turns this:

input type="hidden" value="0" name="check"
input type="checkbox" value="1" name="check"

into an array of values when I test it. Thus Peter Kovacs solution, as correct as it may be or not, does not seem to work with FF 3+.

webwriter
I like better your idea rather than Peter's one. +1
Timotei Dolean
A: 

I just stumbled across this problem myself and I sorted it by updating all values in the database to unchecked then re-checking only the ones that are in the POST data, this works fine for me but might not be everyone's cup of tea.

R Brill