212

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

AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35
Rajasekar
  • 18,392
  • 34
  • 106
  • 137
  • 2
    Even though not asked by OP, [here's](http://stackoverflow.com/a/10833860/239527) a proper solution for having multiple buttons inside one form. – Martti Laine Oct 09 '13 at 15:33

9 Answers9

267

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

Oded
  • 489,969
  • 99
  • 883
  • 1,009
174

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>
ilevent
  • 1,803
  • 1
  • 11
  • 5
  • 13
    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... – Tomas Gonzalez May 12 '15 at 15:35
  • 2
    _3 major browsers support this_ ... the rest is the rest ;) –  Mar 30 '16 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 Mar 22 '17 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 `
    ` tag, and your components are allowed to contain other components. Then you would end up with a `
    ` tag inside another `
    ` tag, which is not allowed not even in HTML5.
    – SantiBailors Jun 02 '17 at 15:04
  • 1
    With this way, to press the ENTER key does not seams to submit the form anymore. Do you confirm? – Gabriel Oct 04 '19 at 11:07
  • Simply brilliant. Helps me solve a nested forms in react problem. – Drew Reese Jan 09 '20 at 08:05
  • @SantiBailors However it allows you to have inputs for different forms in an arbitrary order, which is what a lot of people seem to need. – monamona Oct 25 '22 at 16:01
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).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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

user3007766
  • 161
  • 1
  • 2
  • +1 for making the point that one input can apply to *multiple* forms. A powerful feature that other answers missed. – Stephen R Jan 30 '20 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)

Ben Call
  • 976
  • 1
  • 10
  • 16
  • 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 Aug 07 '13 at 08:38
5

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

manuchap
  • 117
  • 1
  • 5
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

Alex
  • 14,338
  • 5
  • 41
  • 59
  • 5
    The HTML 3 spec isn't a very good one to link to — it died without reaching Recommendation status. – Quentin Aug 07 '10 at 11:27
  • 1
    Why link to an old spec? The Html 3.0? There are newer specs... – Oded Aug 07 '10 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 Aug 07 '10 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 Aug 07 '10 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 Aug 07 '10 at 21:58
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.

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54
GautamD31
  • 28,552
  • 10
  • 64
  • 85
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.