Suppose this checkbox snippet:
<input type="checkbox" value="1">Is it worth?</input>
Is there any reason to statically define the value
attribute of checkboxes in HTML? What does it mean?
The Input Checkbox Value property in HTML DOM is used to set or return the value of the value attribute of an input checkbox field, however the contents of the value attribute does not shown to user. When the form is submitted by the user, the value and the other information sent to the server.
The value attribute is one which all <input> s share; however, it serves a special purpose for inputs of type checkbox : when a form is submitted, only checkboxes which are currently checked are submitted to the server, and the reported value is the value of the value attribute.
When the form is submitted, the data in the value attribute is used as the value of the form input if the checkbox is checked. The default value is "on".
Rendering Checkboxes Checked By DefaultThere is no required value for the checked attribute. However, per the checkbox specification only an empty value or 'checked' are valid. When checked is added to the checkbox element the browser will render it as selected.
I hope I understand your question right.
The value attribute defines a value which is sent by a POST request (i.e. You have an HTML form submitted to a server). Now the server gets the name (if defined) and the value.
<form method="post" action="urlofserver"> <input type="checkbox" name="mycheckbox" value="1">Is it worth?</input> </form>
The server would receive mycheckbox
with the value of 1
.
in PHP, this POST variable is stored in an array as $_POST['mycheckbox']
which contains 1
.
I just wanted to make a comment on Adriano Silva's comment. In order to get what he describes to work you have to add "[]" at the end of the name attribute, so if we take his example the correct syntax should be:
<input type = "checkbox" name="BrandID[]" value="1">Ford</input> <input type = "checkbox" name="BrandID[]" value="2">GM</input> <input type="checkbox" name="BrandId[]" value="3">Volkswagen</input>
Then you use something like: $test = $_POST['BrandID']; (Mind no need for [] after BrandID in the php code). Which will give you an array of values, the values in the array are the checkboxes that are ticked's values.
Hope this helps! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With