[whatwg] non-checked checkbox posting success?

Ian Hickson ian at hixie.ch
Tue Aug 15 03:27:24 PDT 2006

On Mon, 24 Jul 2006, Ric Hardacre wrote:
> When using checkboxes in forms i find myself doing this
> <input type="checkbox" name="foo" value="true">
> <input type="hidden" name="foo" value="false">
> and retrieving the value from the posted form data a bit like this
> if( checkbox.value == "false" )then
> 	bSomeBool = false;
> else	
> 	bSomeBool = true;

Michel then pointed out that this is particularly important when you don't 
know which form controls were submitted.

You can already do all this. Just use a type="hidden" control to list the 
controls that were included:

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

Then, check to see if "present" has the value "foo" (amongst others), and 
if it does, handle "foo.checked".

For cases where you have tables, and each row represents a record with 
multiple controls, just have one hidden control per row, indicating the 
row is present.

This is better than a solution for checkboxes, because it works for other 
controls too, like <select>s, which might not submit a value, or disabled 
text controls, which wouldn't submit a value either.

Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

More information about the whatwg mailing list