-2

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?

Thomas Rollet
  • 1,573
  • 4
  • 19
  • 33
tetris11
  • 817
  • 2
  • 11
  • 27
  • ID is designed to be unique (per element), preventing you from collecting data from multiple-value inputs (checkboxes, multiselects etc.) Without `name`, you'll have to devise your own convention to group the inputs for such use cases, and any convention tends to be non-perfect. ) Also, it's not about PHP; it's about how forms are treated when form is submitted. – raina77ow Feb 02 '16 at 13:03
  • 3
    Related (and almost a duplicate I suppose): [Difference between id and name attributes in HTML](http://stackoverflow.com/questions/1397592/difference-between-id-and-name-attributes-in-html) – raina77ow Feb 02 '16 at 13:07
  • 1
    That's not "parsing the DOM", but getting server-side what form are designed to send from the client-side – Damien Pirsy Feb 02 '16 at 13:15
  • Voting to close as duplicate of question raina77ow mentioned. Although the question mentions PHP, this is a misunderstanding, as PHP can only process what the browser gives it. What the browser provides and why is adequately covered on the existing answers. – IMSoP Feb 02 '16 at 13:22
  • apologies, I did search but thought php was relevant. – tetris11 Feb 03 '16 at 13:28

4 Answers4

4

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. -->
David
  • 208,112
  • 36
  • 198
  • 279
  • I see how PHP is irrelevant (and apologise for that) but I still don't understand why forms dont use `id` as keys instead of `name`. In the duplicate question link, the answer given was that it's useful for checkboxes or other input groups you want overriden, but this seems weak at best. – tetris11 Feb 03 '16 at 13:37
  • 1
    @tetris11: How is that reasoning "weak"? What alternative would you propose? `id` is required to be unique throughout the DOM (as implied by its name, it's an identifier). `name` serves an entirely different purpose, determining the key used in key/value data pairs on HTTP requests. I suppose one could introduce a rule to the HTML spec saying that "in the absence of a `name` attribute, the HTTP request data should use the `id` attribute", but that could add unnecessary complexity and edge cases. The two attributes mean and do different things, so they are different attributes. – David Feb 03 '16 at 13:53
  • but keys are supposed to be unique, and if there's already a specifier providing one (`id`) why not use that instead of relying on a completely different spec? I like your fallback proposal, it makes the most sense to me, because inventing a whole new spec for the sole edge case of having multiple checkboxes seems a little....weak. – tetris11 Feb 03 '16 at 18:00
  • 1
    @tetris11: Keys are unique in the HTTP request, not in the DOM. A single DOM can have multiple forms, often directing to the same action but with different sets of data. Forms can have conditional elements. Etc. My proposal does *not* make sense, it's meant to demonstrate the potential for ambiguity in the spec. A good rule of thumb in software (and probably in many things) is that any one component should have a single responsibility. You're suggesting that one component (`id`) should have two responsibilities, sometimes, maybe. Ambiguities and edge cases are the bane of well-defined specs. – David Feb 03 '16 at 18:04
  • Ah fair point on the multiple forms, didn't think that one through. – tetris11 Feb 03 '16 at 18:09
0

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.

Jai
  • 74,255
  • 12
  • 74
  • 103
  • PHP doesn't use it because it's standard; it uses it *because that's what the browser provides*. PHP has absolutely no choice in the matter, as the IDs are never transmitted to it. – IMSoP Feb 02 '16 at 13:23
  • @IMSoP actually i didn't have correct terms to mention, yours seems a bit better one. – Jai Feb 02 '16 at 13:26
0

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"]')
Ostin
  • 1,511
  • 1
  • 12
  • 25
0

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

Pratik Bhalodiya
  • 736
  • 7
  • 14