91

i.e.:

<form 1>
<input type="hidden" name="url" value="1">
</form 1>

and

<form 2>
<input type="hidden" name="url" value="2">
</form 2>

Is this allowed and valid?

Rohan
  • 1,597
  • 3
  • 15
  • 24
  • Possible duplicate of [HTML form with multiple hidden control elements of the same name](http://stackoverflow.com/questions/452066/html-form-with-multiple-hidden-control-elements-of-the-same-name) – Ciro Santilli OurBigBook.com Jun 01 '16 at 21:43

7 Answers7

79

Yes, it is valid

This is Good

<form name="form1">
  <input type="hidden" name="url" value="1">
</form>

<form name="form2">
  <input type="hidden" name="url" value="2">
</form>

This is also fine and will generally be interpreted as an array of values, e.g. {url: [1, 2]}, depending on what your server does. In a URL encoding, it will look like url=1&url=2.

<form name="form1">
  <input type="hidden" name="url" value="1">
  <input type="hidden" name="url" value="2">
</form>
ZachB
  • 13,051
  • 4
  • 61
  • 89
  • 34
    It is valid. It will create no confusion for the server side language (even PHP, with its conventions for naming fields that share a name, will consistently and predictably handle multiple inputs which don't use that convention). It will create no confusion for JavaScript (which will present the elements as a collection and **not** ignore one of them). It /might/ create confusion for authors who write code without knowing what they are doing, but it is fine by the spec. – Quentin May 25 '10 at 17:58
  • @David Dorward: Thanks for explaining that, I removed that part. –  May 25 '10 at 18:00
  • 15
    This post is misleading. inputs with the same name is the same form are part of the HTML standard - they are used, e.g., for checkboxes. – beldaz Aug 26 '15 at 01:40
  • 1
    It is valid to place multiple inputs (hidden or otherwise) with the same name within a single form. Moreover, it's quite useful when submitting structured lists or associative maps, and is often much cleaner (and less brittle) than hacky DIY serialization. – candu May 18 '16 at 17:56
  • 5
    I know this is an old post, but a point of clarification. PHP needs you to tell it the elements are forming an array like so: If you don't do this, php will take the last value of the last field and plug it into the form array for the key "url". – Mark C. Aug 01 '19 at 12:49
19

Yes.

More, it is essential if you are dealing with radio button groups.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Exactly. Or with checkboxes, in which case you can put [] at the end "url[]" and it creates an array to be picked up. – Kerry Jones May 25 '10 at 17:15
  • 4
    @Kerry — That's a PHPism. Most form handling libraries are quite happy to not have special names for groups of controls. – Quentin May 25 '10 at 17:56
9

"This is Not Good" parses correctly on every browser I know of; if two url's appear in the url encoded string, it will be treated as an array. Try this in JQuery:

$('<form name="form1">\
     <input type="hidden" name="url" value="1">\
     <input type="hidden" name="url" value="2">\
</form>').serialize()

and you will get: "url=1&url=2"

a well-written query string parser will return a json structure like this:

 {"url":["1", "2"]}

Is it strictly spec? Nope, but neither is creating a multi-line string by escaping the EOL with a backslash, as I did above.

Jeff Lowery
  • 2,492
  • 2
  • 32
  • 40
5

Yes -- each will only submit with their respective forms.

If you have them in the same form, one will override the other and it is not valid.

EDIT: As pointed out by Mahmoodvcs that the overriding only occurs in some languages (such as PHP) as is not inherent within HTML itself.

Kerry Jones
  • 21,806
  • 12
  • 62
  • 89
  • 13
    You are wrong. If you have multiple inputs with the same name, one will *not* override the other one. All inputs will be appended to the request's body in the order they appear in html. And it's valid: [HTML form with multiple hidden control elements of the same name](http://stackoverflow.com/questions/452066/html-form-with-multiple-hidden-control-elements-of-the-same-name) – Mahmood Dehghan Jul 28 '15 at 09:26
  • 5
    @Mahmoodvcs: This is not strictly true. If the form is posted to a PHP service, then the last value wins. If you want multiple values assigned to a name, then each name must end in '[]'. – Jeff Lowery Oct 07 '15 at 19:23
  • 6
    We are talking about HTML here, not PHP. – Mahmood Dehghan Oct 09 '15 at 23:00
  • 2
    @yardpenalty -- I don't understand your question (unsure if Jeff will respond), but if you're questioning PHP's ability, this is definitely true (at least prior to PHP7 -- I haven't checked if they've changed that yet) – Kerry Jones Apr 19 '16 at 00:01
  • Sorry I have figured that out. If you are going to process using php you have to make name array. I think it is bootstrap who needs radio buttons to have same name or something. Anyways, yes you can. – yardpenalty.com Apr 19 '16 at 11:59
  • Sorry its jQuery serialize that requires name for form element. Doh! – yardpenalty.com Apr 19 '16 at 12:35
2

To test if it is valid or not, creat you page and test at W3C here :

http://validator.w3.org/

Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174
2

A) Your first example is okay, because the forms time of submission will be different:

<form id="1">
    <input type="hidden" name="url" value="1">  
</form>
<form id="2">
    <input type="hidden" name="url" value="2">  
</form>

B) Your second example is also okay, but not standard coding practice:

<form>
    <input type="hidden" name="url" value="1">  
    <input type="hidden" name="url" value="2">  
</form>

Java code two extract both values:

Map<String,String[]> parmMap = requestObj.getParameterMap();   
String input1 = parmMap.get("url")[0];   
String input2 = parmMap.get("url")[1];
agrm
  • 3,735
  • 4
  • 26
  • 36
1
<form>
    <input type="hidden" name="url[]" value="1">  
    <input type="hidden" name="url[]" value="2">  
</form>

In PHP you will get values with $_POST['url']

for($i=0;$i<count(url);$i++)
echo $_POST['url'][$i];
Rashid
  • 11
  • 4