Is it possible to wrap html form elements in multiple form tags?
Asked Answered
G

2

4

Would it be wrong to have every element of a form wrapped in <form> tags in an HTML page? Am curious as to why it would be wrong:

Basically I have to introduce two forms(form1 and form2) and there are some elements associated with the first while others with the second.The layout has to be completely table based and since the elements of both forms are all over the page here is what i wrote(according to http://validator.w3.org/check, the code is valid):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
 <html>
<head>
   <title>Title of the document</title>
 </head>

 <body>
<table border="1">
    <tr>
        <td>
            <form name="form1" method="POST" action="via.php">
            Enter the first form text here!:    <input type="text" name="first"/>
            </form>
        </td>
        <td rowspan="2">
            <form name="form2" method="POST" action="via.php">
                Enter the second form text here!:<input type="text" name="second"/>
            </form>

            <form name="form1" method="POST" action="via.php">
            Enter the first form text here!:<input type="text" name="first"/>
            </form>
        </td>
  </tr>
  <tr>
        <td>
        <form name="form1" method="POST" action="via.php">
            <input type="submit" name="form1submit" value="submit1"/>
        </form>
        <form name="form12" method="POST" action="via.php">
            <input type="submit" name="form2submit" value="submit2"/>
        </form>
        </td>


  </tr>
</table>

Goatskin answered 24/2, 2012 at 15:25 Comment(0)
L
4

Nested forms are not allowed, according to the specification:

Flow content, but with no form element descendants.

Given this input in the w3 validator:

<!DOCTYPE html><html><head><title>tit</title></head><body>
<form action="x">
    <form action="x">
        <input type="submit" value="x"> <!-- Submitting.. Which form...?-->
    </form>
</form>    
</body></html>

The w3 validator says:

Saw a form start tag, but there was already an active form element. Nested forms are not allowed. Ignoring the tag.

Lewak answered 24/2, 2012 at 15:39 Comment(5)
Yes but im not declaring nested forms, I'm just wrapping elements(textboxes,submit buttons etc) of the same form and i have tried to group thme by using the name variable in the form tag.Goatskin
@Student I see your issue. It is wrong, because, on form submission, only the input elements in the active form are submitted. That is, the form which contained the element from which the submission is initiated.Lewak
so if i have a form <form name="one"><input type="text" name="firsttextbox"/></form> <form name="one"><input type="text" name="secondtextbox"/></form> <form name="one"><input type="submit" value="submit"/></form> which data is gonna get submitted when i click on the submit button? I mean all three forms point to the same form using the name variable? or am i wrong?Goatskin
Multiple <form> elements are permitted. However, you can only submit one HTML form at once. In the comment's case, the server receives either ?firsttextbox=... or ?secondtextbox=..., but not both.Lewak
Thanks @ROB W for the answers!Goatskin
M
2

The question appears to be “Can an HTML document contain several forms?” The correct answer is “Yes, as long as they are not nested.” The forms will be completely distinct, each with a set of fields of its own.

Methedrine answered 24/2, 2012 at 15:36 Comment(2)
Actually it is "Can i have several instances of the form wrapping elements" :) as you can see from the code, there are two forms form1 and form2 and they are multiple instances of these forms(obviously with the same name variable) wrapping elements.Goatskin
@Student, no, your example has four forms. Using the same name attribute for two forms may cause odd effects, but it does not turn them into one form. (In HTML5, you can bind a form field element outside any form into a given form functionally, but that’s a different issue and probably not practical.)Methedrine

© 2022 - 2024 — McMap. All rights reserved.