views:

34

answers:

3

Hi, I went through many online documents for checkbox input in XHTML. Can anyone clear my doubt? I mean 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 when saw that all the checkbox had same name, then why even specify it? A little confused of this. Please Help

+1  A: 

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
@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
@Nagaraj: Yes, that's basically what that attribute is for
Yi Jiang
+3  A: 

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
@JoseK 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
@Nagaraj: yes, as in my update, you can get the *value* (i.e. Milk, chocolate) of the selected checkbox using the *name*
JoseK
A: 

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~~~~~

Matthew Ma