acidzombie24 acidzombie24 - 1 year ago 54
HTML Question

Force a checkbox to always submit, even when unchecked

I have an html form and i would like ALWAYS to have checkboxes to submit a value. How can i do that? I have one idea but i havent tried it and i am unsure if its the best way to do it (jquery to check if the box is checked or not, then set the value to 0/1 and check it off so it will submit)

Answer Source

This goes totally against the natural/specified behaviour of checkboxes in HTML. If checked, then its value will be sent as parameter. If unchecked, then its value will not be sent as parameter. How else would you in the server side be able to distinguish checked from unchecked checkboxes? Note that when the value is unspecified, then most browsers default to "on". It's easier if you give all checkboxes the same name but a different and fixed value. This way you can obtain the checked ones as an array/collection.

If all checkboxes are already known beforehand in server side, you can just apply basic math to obtain the unchecked checkboxes:

uncheckedCheckboxes = allCheckboxes - checkedCheckboxes

That's also the normal practice. If you tell a bit more about the server side language you're using to process the form, we would be able to give more tips/tricks how to achieve this the best way.

If those checkboxes are created dynamically at the client side, then add for each checkbox a <input type="hidden"> field containing information about the checkbox, so that the server side knows which checkboxes are all present as the moment of submission.