[whatwg] non-checked checkbox posting success?

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.

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

Received on Tuesday, 15 August 2006 03:27:24 UTC