为什么PHP只使用'name'标识符解析DOM元素,而不是'id'?

There seems to be a lot of waste in having to duplicate the same string for both the id and name of an element within a form:

<input id="foo" />            <!-- JS/CSS accept this -->
<input name="foo" />          <!-- Only PHP accepts this -->
<input id="foo" name="foo" /> <!-- Everyone's happy -->

Why does PHP not use id tags?

Why does PHP not use id tags?

That's not PHP, that's HTML. PHP has nothing to do with the HTML spec.

HTML does use id attributes, but it uses them for a different purpose than name attributes.

HTML form elements build requests (POST or GET generally) from their child value-carrying elements (input, select, textarea, etc.). The name attribute is used as the key, and the value (or selected value, etc.) is used as the value.

This creates the key/value pairs for the data in the request.

There seems to be a lot of waste in having to duplicate the same string for both the id and name of an element within a form

You don't have to duplicate it. You may personally choose to duplicate it. But that's your choice. There's no rule in any specs/standards/conventions/etc. indicating that you must or even should do that.


<input id="foo" />            <!-- JS/CSS accept this -->
<!--- incorrect.  JS/CSS can also target name attributes if necessary. -->

<input name="foo" />          <!-- Only PHP accepts this -->
<!--- incorrect.  This isn't PHP, this is HTML.  PHP isn't involved here at all. -->

<input id="foo" name="foo" /> <!-- Everyone's happy -->
<!--- if that's the markup you choose to define, sure. -->

Why does PHP not use id tags?

Because that is the standard set for submitting forms to specified serverside. I would say that IDs are most likely to be used with js/css while you need name to serialize your form elements.

Id attribute is used for DOM manipulating. http://www.w3schools.com/tags/att_global_id.asp

When sending forms name attribute is used either. http://www.w3schools.com/tags/att_input_name.asp

These are different purposes. You can use name attributes in JS also, but array of elements will be returned:

// plain JS
document.getElementsByName("foo");

// jQuery example
$('[name="foo"]')

because in web need 3 type identifier for each unique use for example

1) class : its use for designing and also select multiple tag in dom (jquery)
2) id : used to access some specific element in dom and also design for one field
3) name : used for passing single and multiple data and array

AND thing if you can pass value by id then how can you send checkbox value and multiple select tag values ???