What is the purpose of the html name attribute?

advertisements

What is the purpose of the name attribute on input, span, or div tags? Should an id tag be used instead?

Example from Mozilla's documentation:

<label for="User">Click me</label>
<input type="text" id="User" name="Name" />


The name attribute identifies the input value if it is sent to a server via a traditional GET or POST of a form.

Specific to the example if you had:

<form action="http://localhost" method="POST">
    <label for="User">Click me</label>
    <input type="text" id="User" name="Name" value="foo" />
    <input type="submit" value="submit" />
</form>

and you submit the form, the server localhost will receive a content body like:

Name=foo

which as another post mentions, is usually parsed by a server side language like PHP into something easier to work with.

The id attribute identifies the input to the DOM. If you specify an input with no name but an id and try to submit it via a GET or POST it will not be parsed correctly by the server.