views:

102

answers:

9

Why do I need the name and id attributes for <input> form elements?

Which is used for POST data sending and which can I exclude?

+1  A: 

name is used for POST sending

Macros
And GET, and all other means, surely?
David Thomas
Sure, just that OP never said anything about GET.
BoltClock
@ricebowl - good point, all server side protocols will use name
Macros
A fair point, but when I read your answer (having not properly read the question, in all honesty) it seemed as if you were asserting exclusivity for names to POST. My bad =)
David Thomas
+2  A: 

An id isn't required. Name isn't mandatory either, but the browser will not sent the <input>'s data without it. This is the same for POST and GET.

Kobi
Why dont vote..?
piemesons
+8  A: 

name is used by the server-side, this is necessary if you plan to process the field. id is only so label elements, when clicked and accessed by screen-readers, can trigger/invoke the form controls ( inputs, selects ).

<form method=POST action="form-processor.php">
    <input name=first_name value=john>
</form>

results in

$_POST = array( 'first_name' => 'john' );

If the method is GET, it's appended to the query string:

http://site-name.com/form-handler.php?first_name=john

it's popular for query string appending with hidden inputs:

<input type="hidden" name="q" value="1">

meder
Doesn't it result in $_POST = array( 'first_name' => 'john' ); ?
Macros
isn't that what I have?
meder
Haha it is now.....
Macros
A: 

name is needed for post and get... but not id... idis used client side processing...

Reigel
+2  A: 

name is the attribute that determines the "variable name" when doing a post. id is used for javascript purposes etc.

aioobe
A: 

There are no required attributes for an input element.

http://w3schools.com/tags/tag_input.asp - w3schools always has great information as well.

animuson
Wow, people are actually downvoting an answer that is 100% correct? How ignorant.
animuson
Incidentally, I'd like to say that, despite my comments elsewhere, I haven't downvoted, as you say, a hundred-percent correct answer. +1 for correctness, and to restore karma.
David Thomas
A: 

name is required, id is not that important. However, id is used to associate labels to common form input fields like radio button, textboxes, etc.

deostroll
The name attribute is *not* required.
animuson
@animuson, while I agree with you, technically, I'd argue that, if you want to do something with the entered data, it's kind of essential to have a `name` attribute.
David Thomas
@ricebowl: You're basically saying it is necessary in order to perform certain functions, but there are still plenty of uses for an input field where a name attribute would not be needed.
animuson
@animuson: true, but if you need the data in the field to be posted to the server (for processing), the name attribute is required.
deostroll
There is still a difference between requirement and necessity. If you were writing an instruction manual, you would say that you "need to define a name attribute if you are planning to post the data." You don't say a name attribute is required because that gives them the false assumption that they must *always* define a name attribute.
animuson
@animuson, I accept that there are valid uses for a name-less input, but I can't think what they would be; which is the position from which I wrote my previous comment.
David Thomas
A: 

Name is required so that you can post or get the values in the next page. Id is required for you to do manipulations with css and stuffs like that. It is also possible only with the name. So Name is more important. Giving an id makes it look standardised.

1s2a3n4j5e6e7v
Why does giving it an ID make it look 'standardized'? And you really should use 'necessary' instead of 'required'. Word choice is key in programming.
animuson
document.getElementsByName() can be used instead of document.getElementsById() for all client side operations.
Ricardo Slafford
+1  A: 

name is used for POST and GET.

id is used for styling.

class is used for applying the same style to a bunch of elements that are of the same "class".

That's how I memorize them.

Leo Jweda
id is also used to associate a <label> with an <input>, e.g. <label for="foobar">Foobar</label> <input id="foobar">.
Olly Hodgson
Thanks Olly, this is much better than wrapping the <input> with the <label> (Yikes!)
Leo Jweda