HTML form with multiple hidden control elements of the same name

前端 未结 8 2111
-上瘾入骨i
-上瘾入骨i 2020-11-27 06:14

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 i

8条回答
  •  天命终不由人
    2020-11-27 06:54

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

    
    

    ... 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 ColdFusion all expose the values as a comma-separated list. So yes, the duplicate naming is completely valid.)

提交回复
热议问题