views:

3532

answers:

5

Is it legal to have an HTML form with more than one "hidden" control element with the same name? I expect to get the values of all of these elements at the server. If it is legal, do the major browsers implement the behavior correctly?

A: 

Yes you can do that, as long as they have a different id. what is the "correct" behavior you expect?

Victor
I think the correct behavior may be undefined according to the HTML spec, which is why I can't rely on it to work.
Brian
The request only passes name-value pairs; IDs never get there.
Christian Nunciato
someone wants to explain the downvotes?
Victor
+2  A: 

If you want to get them all, and they all have the same name, I'm assuming you're using an array-style name like "colors[]" so they will appear in an array called 'colors' at the server?

<input type="hidden" name="colors[]" value="#003366" />
<input type="hidden" name="colors[]" value="#00FFFF" />
Jonathan Sampson
I've never seen that array syntax before.
Brian
If you are passing a collection of related details through to the server, this method is extremely helpful in that it allows them to travel as a group, rather than numerous independent values.
Jonathan Sampson
The thing with the square brackets is PHP's invention, not any kind of web standard. Other languages and form-reading libraries have different mechanisms for accessing multiple submissions with the same name, not necessarily involving changing the name like this.
bobince
A: 

If you have something like this:

<input type="hidden" name="x" value="1" />
<input type="hidden" name="x" value="2" />
<input type="hidden" name="x" value="3" />

Your query string is going to turn out looking like x=1&x=2&x=3... Depending on the server software you are using to parse the query string this might not work well.

DavGarcia
niels
A: 

Yes, and most application servers will collect the matching elements and concatenate them with commas, such that a form like this:

<html>
<form method="get" action="http://myhost.com/myscript/test.asp"&gt;
<input type="hidden" name="myHidden" value="1">
<input type="hidden" name="myHidden" value="2">
<input type="hidden" name="myHidden" value="3">
<input type="submit" value="Submit">
</form>
</html>

... would resolve to a URL (in the GET case -- POST would work the same way, though) like this:

http://myhost.com/myscript.asp?myHidden=1&amp;myHidden=2&amp;myHidden=3

... and would be exposed to you in code like this: (e.g., following something like Response.Write(Request.QueryString("myHidden")):

1, 2, 3

So to grab the values, you'd just split the string and access them as an array (or whatever's comparable in your language of choice).

(Should be clarified: In PHP, it's slightly different (as Johnathan points out, bracket notation exposes the items as an array to your PHP code), but ASP, ASP.NET and CF all expose the values as a comma-separated list. So yes, the duplicate naming is completely valid.)

Christian Nunciato
"most"? Well, one perhaps...
bobince
Sorry, I should've been more specific: "everything except PHP." ;) ASP, ASP.NET, CF all operate this way. PHP privileges the final item in the list, unless the bracket notation is specified, as Johnathan mentions in his answer. The point is that the HTML syntax is valid.
Christian Nunciato
Added the PHP note for clarification.
Christian Nunciato
So everything except PHP, Perl CGI.pm, Python cgi.py, htmlform, django etc., Ruby on Rails, Java Servlet...
bobince
+4  A: 

The browsers are OK with it. However, how the application library parses it may vary.

Programs are supposed to group identically named items together. While the HTML specification doesn't explicitly say this, it is implicitly stated in the documentation on checkboxes:

Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property.

R. Bemrose