I am creating a form in jsp.

<form action="" method="post"> 
<input type="hidden" name="status_1" value="0" /> 
<input type="checkbox" id="status_1" name="status_1" value="1" />
<input type="hidden" name="status_2" value="0" /> 
<input type="checkbox" id="status_2" name="status_2" value="1" />
<input type="hidden" name="status_3" value="0" />
<input type="checkbox" id="status_3" name="status_3" value="1" />
<input type="submit" /> </form>

I was referring this link: http://stackoverflow.com/questions/19239536/how-get-value-for-unchecked-checkbox-in-checkbox-elements-when-form-posted

So, can you tell how this is priortized? I mean if I am marking chekcbox as checked, then how does it decide which value to take in the req parameter?

Also, is there any order dependent thing in this? if hidden input is written after the checkbox or before this?

Note: Although this answer is accepted, but it doesn't work for me if I place hidden input before checkbox. When I say not working means it was always taking hidden input value when I place hidden input before checkbox with same name.

Thanks in advance.

Edited by nitin1

2 Years
Discussion Span
Last Post by AndrisP

In your example you trying send to server two variables by same name. I think more convenient is array of checkboxes with binary step values e.g.

<form action="" method="post"> 
<input type="checkbox" id="status_1" name="status[]" value="1" />
<input type="checkbox" id="status_2" name="status[]" value="2" />
<input type="checkbox" id="status_3" name="status[]" value="4" />
<input type="submit" />

then on the server side you get value as sum of array e.g.

$status = ( isset($_POST['status']) ? array_sum($_POST['status']) : 0 );

and then compare as binary e.g.

if($status & 1){ /* statement for option 1 */ }
if($status & 2){ /* statement for option 2 */ }
if($status & 4){ /* statement for option 3 */ }

Edited by AndrisP

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.