216

Whether we can have a form inside another form?. Is there any problem with that.

1
  • 2
    Even though not asked by OP, here's a proper solution for having multiple buttons inside one form. Commented Oct 9, 2013 at 15:33

9 Answers 9

274

Though you can have several <form> elements in one HTML page, you cannot nest them.

0
186

Form nesting can be achieved with new HTML5 input element's form attribute. Although we don't nest forms structurally, inputs are evaluated as they are in their own form. In my tests, 3 major browsers support this except IE(IE11). Form nesting limitation was a big obstacle for HTML UI design.

Here is a sample code, when you click Save button you should see "2 3 success" (Original http://www.impressivewebs.com/html5-form-attribute/):

<form id="saveForm" action="/post/dispatch/save" method="post"></form>
<form id="deleteForm" action="/post/dispatch/delete" method="post"></form>

<div id="toolbar">
    <input type="text" name="foo" form="saveForm" />
    <input type="hidden" value="some_id" form="deleteForm" />
    <input type="text" name="foo2" id="foo2" form="saveForm" value="success" />

    <input type="submit" name="save" value="Save" form="saveForm" onclick="alert(document.getElementById('deleteForm').elements.length + ' ' + document.getElementById('saveForm').elements.length + ' ' + document.getElementById('saveForm').elements['foo2'].value);return false;" />
    <input type="submit" name="delete" value="Delete" form="deleteForm" />
    <a href="/home/index">Cancel</a>
</div>
7
  • 14
    This sure is what we've all been waiting for! Shame for IE... always behind the rest... That makes this great idea useless for public websites... Commented May 12, 2015 at 15:35
  • 2
    3 major browsers support this ... the rest is the rest ;)
    – user4227915
    Commented Mar 30, 2016 at 19:48
  • 1
    This is good to know. I've implemented it in the past with custom data attributes and javascript when I could have just included a html5 shim library.
    – Jon Hulka
    Commented Mar 22, 2017 at 18:10
  • 20
    -1 This is not form nesting at all. The two forms are not nested, none of them is inside the other one, which was the question. Form nesting would be useful f.ex. when you write different kinds of components (in any language) including some that can receive user input, so such component's resulting HTML contains a <form> tag, and your components are allowed to contain other components. Then you would end up with a <form> tag inside another <form> tag, which is not allowed not even in HTML5. Commented Jun 2, 2017 at 15:04
  • 1
    With this way, to press the ENTER key does not seams to submit the form anymore. Do you confirm?
    – Gabriel
    Commented Oct 4, 2019 at 11:07
85

No. HTML explicitly forbids nested forms.

From the HTML 5 draft:

Content model: Flow content, but with no form element descendants.

From the HTML 4.01 Recommendation:

<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->

(Note the -(FORM) section).

0
16

Nested forms are not supported and are not part of the w3c standard ( as many of you have stated ).

However HTML5 adds support for inputs that don't have to be descendants of any form, but can be submitted in several forms - by using the "form" attribute. This doesn't exactly allow nested forms, but by using this method, you can simulate nested forms.

The value of the "form" attribute must be id of the form, or in case of multiple forms, separate form id's with space.

You can read more here

1
  • +1 for making the point that one input can apply to multiple forms. A powerful feature that other answers missed.
    – Stephen R
    Commented Jan 30, 2020 at 15:49
8

If you have a master form and are forced to have a "form with a form" Here is what you can do... in my case I had a link in the globalHeader and I wanted to perform a post when it was clicked:

Example form post with link button submit:

Instead of a form... wrap your input in a div:

 <div id="gap_form"><input type="hidden" name="PostVar"/><a id="myLink" href="javascript:Form2.submit()">A Link</a></div>

js file:

$(document).ready(function () {
(function () {
    $('#gap_form').wrap('<form id="Form2" action="http://sitetopostto.com/postpage" method="post" target="_blank"></form>');
})();});

This would wrap everything inside the div "gap_form" inside a form on the fly and the link would submit that form. I have this exact example working on a page now... (In my example...You could accomplish the same thing by redirecting to a new page and submitting the form on that page... but I like this better)

1
  • 25
    Ok, downvote and explanation. (1) This does not resolve the inherent problem of a "form within a form". Yeah, you've tricked the browser into nesting forms. Congrats. But now your document is invalid, and the browser has license to do whatever it damn well pleases. And i guarantee there's a browser somewhere that'll throw an error. (2) This breaks all to hell if for any reason JS is disabled, the scripts don't want to load, or whatever. Maybe you don't care about that, but it is crappy design to rely on JS to fix your mess rather than creating valid HTML in the first place.
    – cHao
    Commented Aug 7, 2013 at 8:38
5

Yes there is. It is wrong. It won't work because it is wrong. Most browsers will only see one form.

http://www.w3.org/MarkUp/html3/forms.html

5
  • 5
    The HTML 3 spec isn't a very good one to link to — it died without reaching Recommendation status.
    – Quentin
    Commented Aug 7, 2010 at 11:27
  • 1
    Why link to an old spec? The Html 3.0? There are newer specs...
    – Oded
    Commented Aug 7, 2010 at 11:27
  • This one is the only one that specifies at the beginning of the description about this interdiction (and one of the first Google Search results). In other cases I've only seen forum discussions about HTML4 and XHTML leading to the same results. One forum post even said something about HTML4 allowing this, though I never heard about it and couldn't find any examples or proof.
    – Alex
    Commented Aug 7, 2010 at 11:31
  • @Alexander — "This one is the only one that specifies at the beginning of the description about this interdiction" — err? I linked to two others in my answer.
    – Quentin
    Commented Aug 7, 2010 at 13:30
  • 2
    What's the point? The right answer was the one which had absolutely no justification whatsoever. I am beginning to doubt the effectiveness of participating in this.
    – Alex
    Commented Aug 7, 2010 at 21:58
5

Another workaround would be to initiate a modal window containing its own form

1
  • Do you have an example? Commented Apr 26, 2017 at 16:48
4

No we can't nest forms within another form like this

<form name='form1'>
      <form name='form2'>
            //some code here
      </form>
</form>

it will work in some cases but it is not recommended for universal platforms. You can use plenty of SUBMIT buttons inside a single form, but you can't manage a nested form appropriately.

1
3

It's not valid XHTML to have to have nested forms. However, you can use multiple submit buttons and use a serverside script to run different codes depending on which button the users has clicked.

Not the answer you're looking for? Browse other questions tagged or ask your own question.