Gurgaon Jobs, Companies and Consultants

Those who are looking for job in Gurgaon can visit this site : Gurgaon Jobs. Here you will find email ids/contact no of "Companies in Gurgaon" and also of "Placement Consultants in Gurgaon"

Friday, May 22, 2009

Unchecked checkbox problem value not submitted with form - Trick to get it done

Unselected checkbox value not submitted with form

Problem definition :


Today i encountered with one of rule of Html Specification :

unselected radio buttons and checkboxes are not submitted with form.

That means if you have a boolean property xyz in form that toggles checkbox, in that case
value for xyz will be submitted only if it is checked otherwise not.

So in case of updating existing record suppose your field xyz was set to true and now user unchecks that then on form submission value of xyz will remain true, it will not become false as expected.

Solution :

Use a hidden variable of same name and with value as false. Remember your hidden variable should come after the checkbox.

Example :

Your Old code :
<input name="xyz" type="checkbox"/>

Your New Code :
<input name="xyz" type="checkbox"/>
<input name="xyz" value="false" type="hidden"/>

This will give you correct behaviour which you expect. That is if user checks the box then xyz value should be true and if user unchecks then xyz value should be false.

Other approaches which people suggest is to write some javascript for
unchecked boxes to explictly submit them Or use radio button etc,
its upto you to choose whichever works for you.

If this does not work for some one please leave a comment on this post.

5 comments:

Unknown said...

Thanks Naresh.....
your post help me to save my 2 days time

GRAJ said...

Im using this way to solve it:

http://j2eetiger.blogspot.com/2010/02/solution-how-to-get-your-browser-to.html

cheers

Matt Bodman said...

Simple and brilliant... thank you!

Nagendra said...

Good catch dude...

Unknown said...

sir i am final year student of b.e but my b.e percentage is very low so i want do b.e frm amie. I need ur suggessions ,,,pllzz