Is it OK to have multiple HTML forms with the same name?

forms, html

Solution

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.

Problem

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?

Original source

Related problems