Is it standard behaviour for browsers to only send the checkbox input value data if it is checked upon form submission?
And if no value data is supplied, is the default value always "on"?
Assuming the above is correct, is this consistent behaviour across all browsers?
Yes, because otherwise there'd be no solid way of determining if the checkbox was actually checked or not (if it changed the value, the case may exist when your desired value if it were checked would be the same as the one that it was swapped to).
Other answers confirm that "on" is the default. However, if you are not interested in the value, just use:
In HTML, each
<input />
element is associated with a single (but not unique) name and value pair. This pair is sent in the subsequent request (in this case, a POST request body) only if the<input />
is "successful".So if you have these inputs in your
<form>
DOM:Will generate these name+value pairs which will be submitted to the server:
Notice that:
two
andsix
were excluded because they had thedisabled
attribute set.three
was sent twice because it had two valid inputs with the same name.four
was not sent because it is acheckbox
that was notchecked
six
was not sent despite beingchecked
because thedisabled
attribute has a higher precedence.seven
does not have aname=""
attribute sent, so it is not submitted.With respect to your question: you can see that a checkbox that is not checked will therefore not have its name+value pair sent to the server - but other inputs that share the same name will be sent with it.
Frameworks like ASP.NET MVC work around this by (surreptitiously) pairing every
checkbox
input with ahidden
input in the rendered HTML, like so:Renders:
If the user does not check the checkbox, then the following will be sent to the server:
If the user does check the checkbox, then both will be sent:
But the server will ignore the
=false
version because it sees the=true
version, and so if it does not see=true
it can determine that the checkbox was rendered and that the user did not check it - as opposed to theSomeBooleanProperty
inputs not being rendered at all.Having the same problem with unchecked checkboxes that will not be send on forms submit, I came out with a another solution than mirror the checkbox items.
Getting all unchecked checkboxes with
None of the above answers satisfied me. I found the best solution is to include a hidden input before each checkbox input with the same name.
<input type="hidden" name="foo[]" value="off"/>
<input type="checkbox" name="foo[]"/>
Then on the server side, using a little algorithm you can get something more like HTML should provide.
This will be the raw input
And the function will return
I resolved the problem with this code:
HTML Form
and the javascript function by change the checkbox value form:
and the server checked if the data post is "on" or "off". I used playframework java
Yes, standard behaviour is the value is only sent if the checkbox is checked. This typically means you need to have a way of remembering what checkboxes you are expecting on the server side since not all the data comes back from the form.
The default value is always "on", this should be consistent across browsers.
This is covered in the W3C HTML 4 recommendation: