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?
Yes you can do that, as long as they have a different id. what is the "correct" behavior you expect?
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" />
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.
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">
<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&myHidden=2&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.)
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.