26

I went through many online documents for the checkbox input in XHTML. Can anyone clear my doubt? What does this name field actually stand for?

Milk: <input type="checkbox" name="checkbox" value="Milk">
Chocolate: <input type="checkbox" name="checkbox" value="chocolate">
Cold Drink: <input type="checkbox" name="checkbox" value="Cold Drink">

I thought it was an identifier for that particular checkbox, which can later be used in other file by just referring their name, but given that all the checkbox had same name, why even specify it? A little confused of this.

arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
Nagaraj Tantri
  • 5,172
  • 12
  • 54
  • 78

5 Answers5

35

Dont be confused because of name="checkbox". It could more logically be name="drink" and type=checkbox.

In the above case, you have multiple checkboxes with the same name. When several checkboxes have the same name, the form will send a group of values to the server in the request. Note: only the values of the checked checkboxes will be sent to the server.

Ideally these are used to allow multiple choice questions where more than one answer is allowed. As opposed to radio buttons, where only one answer is allowed among the options.

Update:

On the receiving side, if you're using JSP for example - the values of the selected checkboxes will be available as request.getParameterValues("drink") or request.getParameterValues("checkbox") in your actual case. This is where the name attribute is used.

JoseK
  • 31,141
  • 14
  • 104
  • 131
  • so now if i want to use a particular checkbox value and for time being neglect others, i use name attribute as a identifier? – Nagaraj Tantri Sep 02 '10 at 12:15
  • @Nagaraj: yes, as in my update, you can get the *value* (i.e. Milk, chocolate) of the selected checkbox using the *name* – JoseK Sep 02 '10 at 12:20
10

The name attribute is used to reference form data after it’s submitted, and to reference the data using JavaScript on the client side.

Source: http://reference.sitepoint.com/html/input/name

Basically, what you've described. When the form is submitted, you can access the values of the form elements through the name you ascribe to them.

The only place where you would want to have multiple inputs with the same name is when they are radio buttons, in which case it is used to indicate which one of them belongs to the same group and thus only one of which can be selected at a time.

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
  • @Yi which means, name does stand for identifier which i can use else where in order to access the value of that checkbox? – Nagaraj Tantri Sep 02 '10 at 12:12
  • @Nagaraj: Yes, that's basically what that attribute is for – Yi Jiang Sep 02 '10 at 12:13
  • "The only place where you would want to have multiple inputs with the same name is when they are radio buttons" – That's not true. Consider, for example, a survey that asks which types of animals you have had as pets. Giving them all the same name allows them to be groups in the form data and easily looped over on the server. – Quentin Oct 25 '22 at 13:46
0

The name attribute is used to identify a checkbox. Which you could parse into a object like so {checkboxName1: 'checkboxValue2', checkboxName2: 'checkboxValue2'}

-1

You missed the array setting for the name. By using the array setting (using square brackets), the result will be three different indexes for the checkboxes.

Milk: <input type="checkbox" name="checkbox[]" value="Milk">
Chocolate: <input type="checkbox" name="checkbox[]" value="chocolate">
Cold Drink: <input type="checkbox" name="checkbox[]" value="Cold Drink">
  • That (a) isn't what the question is asking about and (b) is not true in the general case (a small number of form data parses, notably PHP's, work that way, but most do not). – Quentin Oct 25 '22 at 13:45
-3

the "name" is same with the databse record, every field should have a name, so when you click the submit, the data will recorded to the database~~~~~