What is the difference between the id
and name
attributes? They both seem to serve the same purpose of providing an identifier.
I would like to know (specifically with regards to HTML forms) whether or not using both is necessary or encouraged for any reasons.
See this http://mindprod.com/jgloss/htmlforms.html#IDVSNAME
NOTE: attribute "name" for some tags like
<a>
is not supported in HTML5.The way I think about it and use it is simple:
id is used for CSS and JavaScript/jQuery (has to be unique in a page)
name is used for form handling in PHP when a form is submitted via HTML (has to be unique in a form - to some extent, see Paul's comment below)
ID i used to uniquely identify an element.
Name is used in forms. when even u submit a form. and if you dont give any name, nothing will will be submitted. And the ones only with name attribute will go out.
The ID of a form input element has nothing to do with the data contained within the element. IDs are for hooking the element with JavaScript and CSS. The name attribute, however, is used in the HTTP request sent by your browser to the server as a variable name associated with the data contained in the value attribute.
For instance:
When the form is submitted, the form data will be included in the HTTP header like this:
If you add an ID attribute, it will not change anything in the HTTP header. It will just make it easier to hook it with CSS and JavaScript.
Below is an interesting use of the id attribute. It is used within the tag and used to identify the form for elements outside of the boundaries so that they will be included with the other fields within the form.