Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the value attribute mean for checkboxes in HTML?

Tags:

html

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?

like image 485
Metalcoder Avatar asked Jan 14 '13 17:01

Metalcoder


People also ask

What is value in checkbox in HTML?

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.

What is the use of value attribute in checkbox?

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.

What should a checkbox value be?

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".

Do checkboxes need a value?

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.


2 Answers

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.

like image 80
user1116033 Avatar answered Sep 30 '22 23:09

user1116033


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! :)

like image 38
Nift Avatar answered Sep 30 '22 22:09

Nift