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?
Regarding the HTML 4.01 specication, you can use
formelements with the samenameattribute, 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>, thendocument.foorefers to that form.It is undefined what happens when same
nameattribute is used, but what browsers seem to do is to return an array. In your example,document.foowould be a 3-element array, withdocument.foo[0]being the first form. But this is not useful, since (assuming there are no other forms in the document) you could usedocument.forms[0], with a well-defined meaning.The
nameattribute itself is outdated forformelements (but not for form fields, where it keeps being essential). The HTML 4.01 spec clause onformsays:“
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
idattribute to identify elements.”In the HTML5 drafts, even the formal rules disallow the use of the same
nameattribute. The HTML5 clause on thenameattribute onformsays that its value “must be unique amongst theformelements in theformscollection that it is in, if any”. This is a confusing formulation, but it is safest to assume that it must be unique within theformelements of a document.