November 14th, 2007

Can anyone explain to me why an input of type checkbox does not submit when not checked? The only thing I can think off is because someone, somewhere, at the beginning of time thought it would be nicer to have to just check if the parameter is set, instead of checking its actual state. And someone else, somewhere else probably came along during revision time and said, “Hey this is stupid, but we can’t change it now since it would break everybody’s code.”

And now here we are, in 2007, unable to use checkbox arrays to act as rows in a column submitted as a result set with other rows and columns. There are a bunch of ways to overcome this limitation with javascript, and I have nothing against javascript. But dammit, why can’t HTML just support this? At least give me a way to specify a value for an unchecked checkbox; it can still default to nothing.

Not everybody has javascript turned on, and if it’s not, then what? I can’t allow the form submission, period? Pardon my French, but bullshit.

Comments please.

6 comments

The most common “workaround” is using hidden form fields. Using the values of these hidden fields you could check if the corresponding checkbox is set. This can all be done serverside.

I’m not exactly sure what you’re trying to do but I’m pretty sure there’s no need for javascript.

I’m not sure I follow, though I have tried different variations of hidden input fields. The problem that seems to persist is that the hidden field cannot be modified without using javascript (or some client side script).

Basically a scenario is:

<!-- record number 1 -->
<input type="text” name="name[]" />
<input type="checkbox” name="allowed[]" />

<!-- record number n -->
<input type="text” name="name[]" />
<input type="checkbox” name="allowed[]" />

So in the situation I basically have an arbitrary number of records for users, each with a name column and an allowed column, for the sake of this example. Every record can be edited at once, and I don’t want to name each checkbox specifically, just to refer to the entire column as an array.

Even if I were to add another hidden input, the hidden input values would not correspond to the checkbox values as they are toggled, without some form of client side script to do so. Example below:

<input type="text” name="name[]" />
<input type="checkbox” name="allowed[]" />
<input type="hidden” name="allowed_hidden[]" />

In the case then where there are three records, and only the second record has the allowed checkbox checked, I’ll get a name array with three values, an allowed_hiden array with three values, and an allowed array with one value, with no way to tie the single value in the allowed array to a specific row, as I need to.

Can you give an example of exactly what you are referring to? If there is a simple way of getting around this without client side scripting, I’m all ears.

Assuming that every user has an ID - since you are talking about editing I guess you are retrieving them from a database.

What about making the user ID the value of the checkbox and the value of the hidden fields. That way you can loop through the checkbox array and figure out, by means of user id, which user had its checkbox checked and which user did not.

Am I making any sense?

Another way, about which I’m not sure, is indexing the array yourself… I’m not sure how PHP (which I guess you are using) handles name=”allowed_hidden[0]” name=”allowed_hidden[2]” etc. But if it keeps your manual indexing it should automagically give you an array with null values for the fields that weren’t checked.

Jaap, your second solution is perfect; because PHP (yes, I’m using PHP) uses associative arrays, the submission can produce an array such as the following, which is in effect “null values”:

Array
(
[test] => Array
(
[1] => on
[4] => on
)

)

I can definitely make this work, and without client side scripting. Thanks!

Very useful page…I was facing a similar issue.

Honi soit look closer buy cytotec then announced daughters.

Leave a Reply