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 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With