21

I have a valid reason for wanting to do this, but it's a long story so I'll forgot trying to explain why and just ask if it's OK to do.

I have a page where I need to have multiple forms with the same name, but I only want the form whose submit button is click to be submitted. For example, the following might be on my page:

<form name="input" action="" method="get">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>

text

<form name="input" action="" method="get">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>

text

<form name="input" action="" method="get">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>

Is this acceptable?

Nate
  • 26,164
  • 34
  • 130
  • 214

4 Answers4

22

Regarding the HTML 4.01 specication, you can use form elements with the same name attribute, as there is no uniqueness requirement on them. Doing so defeats the purpose of such attributes, though. They are meant for making it easier to refer to forms in client-side scripting: if you have <form name=foo>, then document.foo refers to that form.

It is undefined what happens when same name attribute is used, but what browsers seem to do is to return an array. In your example, document.foo would be a 3-element array, with document.foo[0] being the first form. But this is not useful, since (assuming there are no other forms in the document) you could use document.forms[0], with a well-defined meaning.

The name attribute itself is outdated for form elements (but not for form fields, where it keeps being essential). The HTML 4.01 spec clause on form says:

name = cdata [CI] This attribute names the element so that it may be referred to from style sheets or scripts. Note. This attribute has been included for backwards compatibility. Applications should use the id attribute to identify elements.”

In the HTML5 drafts, even the formal rules disallow the use of the same name attribute. The HTML5 clause on the name attribute on form says that its value “must be unique amongst the form elements in the forms collection that it is in, if any”. This is a confusing formulation, but it is safest to assume that it must be unique within the form elements of a document.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • The bit about document.foo for the multiple matching names case being undefined, isn't that what section 3.1.4 DOM tree accessors from http://dev.w3.org/html5/spec/dom.html#dom-document-namedItem-which to the end of 3.1.4 defines? – Alohci Jun 20 '12 at 06:39
  • @Alohci, it’s undefined in current specs. The HTML5 drafts define a mechanism for this, but it also imposes a uniqueness requirement. – Jukka K. Korpela Jun 20 '12 at 08:35
  • Yeah, fair point. Discussions of what's HTML 4, what's HTML5, and what user-agents *do*, given that they don't have separate HTML 4 and HTML5 modes, gets very complicated sometimes. – Alohci Jun 20 '12 at 09:20
11

Yes it is allowed, only id's must be unique. I wouldn't recommend it however, why even put yourself in a position to be confused down the road.

The name attribute only defines what each form field element will be represented as when sent to the server.

secretformula
  • 6,414
  • 3
  • 33
  • 56
  • 8
    The `name` attribute of a `form` element does not affect the data sent to the server. You may have confused it with `name` attributes for form fields. – Jukka K. Korpela Jun 20 '12 at 03:30
1

It is also ok in HTML5. Only the name must be unique inside the form itself.

See the docs: "The value must not be the empty string, and the value must be unique amongst the form elements in the forms collection that it is in, if any."

  • 1
    This may be misleading. In HTML5, the [W3C requires](https://www.w3.org/TR/html5/forms.html#attr-form-name) **a form's** `name` to be unique in a collection of forms, if any. But the elements' `name`s in a form can be [any non-empty string](https://www.w3.org/TR/html5/forms.html#attr-fe-name) except \__charset\__ and _isindex_. – Quoting Eddie Apr 13 '16 at 13:33
0

When the user clicks a submit button, only that form will be taken in action. Still, it should be better to name them so that you are not confused :)

Erenor Paz
  • 3,061
  • 4
  • 37
  • 44