Is it valid to have an HTML form inside another HTML form? [duplicate]
Asked Answered
N

15

393

Is it valid HTML to have the following:

<form action="a">
    <input.../>
    <form action="b">
        <input.../>
        <input.../>
        <input.../>
    </form>
    <input.../>
</form>

So when you submit "b" you only get the fields within the inner form. When you submit "a" you get all fields minus those within "b".

If it isn't possible, what workarounds for this situation are available?

Nucleoprotein answered 17/2, 2009 at 8:33 Comment(3)
It seems to me that this is actually a very common need that is familiar from db interfaces -- If a form updates table A, and that table has a field linked to table B, we often want a way to update or create entries for that linked field without having to leave the current form. Nested sub-forms would be a very intuitive way to do it (and is the ui implemented by several desktop databases).Sylvanite
It isn't valid. There are workarounds but you should be using another method to obtain this data. Consider one form sending all the data to a PHP mail script, which then submits part (the part from a) as one email, and part (the part from b) as another email. Or into a database, or whatever you're doing with this data. Nesting forms can be done but it is NOT the answer!Tallyman
The question is good, but a quick google search (without clicking the provided links even) reveals whether forms within forms is valid or not. i, personally, liked @Andreas answer.Apostatize
N
438

A. It is not valid HTML nor XHTML

In the official W3C XHTML specification, Section B. "Element Prohibitions", states that:

"form must not contain other form elements."

http://www.w3.org/TR/xhtml1/#prohibitions

As for the older HTML 3.2 spec, the section on the FORMS element states that:

"Every form must be enclosed within a FORM element. There can be several forms in a single document, but the FORM element can't be nested."

B. The Workaround

There are workarounds using JavaScript without needing to nest form tags.

"How to create a nested form." (despite title this is not nested form tags, but a JavaScript workaround).

Answers to this StackOverflow question

Note: Although one can trick the W3C Validators to pass a page by manipulating the DOM via scripting, it's still not legal HTML. The problem with using such approaches is that the behavior of your code is now not guaranteed across browsers. (since it's not standard)

Nonlinearity answered 17/2, 2009 at 8:44 Comment(1)
See my comment up above.... This is a wonderful answer, great work-around, but a terrible practice. You can do the exact same thing with less hassle submitting all the data to a PHP script and dividing and sending to their own destinations from there. Although, you did answer the question which is great, it's just a bad and pointless practice because you could do it the right way in less time.Tallyman
U
76

In case someone find this post here is a great solution without the need of JS. Use two submit buttons with different name attributes check in your server language which submit button was pressed cause only one of them will be sent to the server.

<form method="post" action="ServerFileToExecute.php">
    <input type="submit" name="save" value="Click here to save" />
    <input type="submit" name="delete" value="Click here to delete" />
</form>

The server side could look something like this if you use php:

<?php
    if(isset($_POST['save']))
        echo "Stored!";
    else if(isset($_POST['delete']))
        echo "Deleted!";
    else
        echo "Action is missing!";
?>
Ugrian answered 31/5, 2012 at 12:42 Comment(4)
This is not the same, in this case you want to go to the same page and do different actions, with 2 forms you can go to different pages And/ OR have different information sent in each case.Oribel
You could use a php file as wrapper and include any file you want if you want the code in different files. So instead of (echo "stored!";) you could write (include "a.php";)Ugrian
I have multiple delete buttons in my form (the reason i was coming here considering a nested form) 1 for each record and a list checkbox on each to do a bulk delete. Your response has prompted me to rethink my plan im thinking now that i should name the buttons for the individual delete with a numeric id and the bulk delete as that. Il make php do the sorting.Nefertiti
@Ugrian is on the money. This is the natural solution to vary how form input is interpreted. If you use Post/Redirect/Get then the destination can vary also. However, if you don't have the flexibility on the server side to combine your actions into a single 'aORb' endpoint then I think you're stuck with a hack I'm afraid.Lenin
K
40

HTML 4.x & HTML5 disallow nested forms, but HTML5 allows a workaround with the "form" attribute ("form owner").

As for HTML 4.x you can:

  1. Create additional hidden forms, then use JavaScript to copy data into one of hidden forms and submit it.
  2. Use CSS to line up several HTML form to look like a single entity - but it might be complicated to do.
Karren answered 13/5, 2010 at 21:59 Comment(3)
Not sure why this was down-voted. Here the link to the W3C section of form owners: w3.org/TR/html5/…Galipot
@Galipot your link is out of date, here is the new one w3.org/TR/html5/forms.html#form-ownerDomicile
You need to explain more about how a "form owner" works.Agone
N
16

As others have said, it is not valid HTML.

It sounds like your are doing this to position the forms visually within each other. If that is the case, just do two separate forms and use CSS to position them.

Noaccount answered 17/2, 2009 at 8:47 Comment(1)
This is what I was thinking I would need for my website,but would love an example of how to do this.Eruptive
G
11

No, the HTML specification states that no FORM element should contain another FORM element.

Gordongordy answered 17/2, 2009 at 8:37 Comment(0)
E
7

A possibility is to have an iframe inside the outer form. The iframe contains the inner form. Make sure to use the <base target="_parent" /> tag inside the head tag of the iframe to make the form behave as part of the main page.

Ensure answered 27/2, 2012 at 17:19 Comment(0)
N
6

rather use a custom javascript-method inside the action attribute of the form!

eg

<html>
    <head>
        <script language="javascript" type="text/javascript">
        var input1 = null;
        var input2 = null;
        function InitInputs() {
            if (input1 == null) {
                input1 = document.getElementById("input1");
            }
            if (input2 == null) {
                input2 = document.getElementById("input2");
            }

            if (input1 == null) {
                alert("input1 missing");
            }
            if (input2 == null) {
                alert("input2 missing");
            }
        }
        function myMethod1() {
            InitInputs();
            alert(input1.value + " " + input2.value);
        }
        function myMethod2() {
            InitInputs();
            alert(input1.value);
        }
        </script>
    </head>
    <body>
        <form action="javascript:myMethod1();">
            <input id="input1" type="text" />
            <input id="input2" type="text" />
            <input type="button" onclick="myMethod2()" value="myMethod2"/>
            <input type="submit" value="myMethod1" />
        </form>
    </body>
</html>
Neoteny answered 17/2, 2009 at 8:39 Comment(0)
L
5

You can answer your own question very easily by inputting the HTML code into the W3 Validator. (It features a text input field, you won't even have to put your code on a server...)

(And no, it won't validate.)

Landing answered 17/2, 2009 at 8:41 Comment(0)
T
5

As workaround you could use formaction attribute on submit button. And just use different names on your inputs.

<form action="a">
<input.../>
    <!-- Form 2 inputs -->
    <input.../>
    <input.../>
    <input.../>
    <input type="submit" formaction="b">

</form>
<input.../>
Thrasher answered 31/7, 2020 at 9:50 Comment(2)
What if the methods are different though? If I need a POST method for one form and a GET method for another it won't work.Pearson
@delivey probably a bit late for you, but the documentation linked by @Thrasher also shows a formmethod attribute see developer.mozilla.org/en-US/docs/Web/HTML/Element/input/…Brocky
F
3

no, see w3c

Footwear answered 17/2, 2009 at 8:37 Comment(0)
L
2

No, it is not valid. But a "solution" can be creating a modal window outside of form "a" containing the form "b".

<div id="myModalFormB" class="modal">
    <form action="b">
        <input.../>
        <input.../>
        <input.../>
        <button type="submit">Save</button>
    </form>
</div>

<form action="a">
    <input.../>
    <a href="#myModalFormB">Open modal b </a>
    <input.../>
</form>

It can be easily done if you are using bootstrap or materialize css. I'm doing this to avoid using iframe.

Labrador answered 6/8, 2017 at 17:41 Comment(0)
K
2

Fast Solution: To obtain different validations to different forms and keep their submits in separated functions you can do this:

<form id="form1" onsubmit="alert('form1')"></form>
<form id="form2" onsubmit="alert('form2')"></form>

<div>
  <input form="form1" required />
  <input form="form1" required />

  <div>
      <input form="form2" required />
      <input form="form2" required />
      <button form="form2" type="submit">Send form2</button>
  </div>

  <input form="form1" required />
  <button form="form1" type="submit">Send form1</button>
</div>
Kujawa answered 5/11, 2022 at 17:16 Comment(0)
B
0

A non-JavaScript workaround for nesting form tags:

Because you allow for

all fields minus those within "b".

when submitting "a", the following would work, using regular web-forms without fancy JavaScript tricks:

Step 1. Put each form on its own web page.

Step 2. Insert an iframe wherever you want this sub-form to appear.

Step 3. Profit.

I tried to use a code-playground website to show a demo, but many of them prohibit embedding their websites in iframes, even within their own domain.

Benediction answered 1/6, 2020 at 0:2 Comment(0)
S
0

You are trying to implement nested form which is not supported in HTML.

Every form must be enclosed within a FORM element. There can be several forms in a single document, but the FORM element can't be nested.

Workaround

You can implement this functionality with some change in HTML and JavaScript. (without using html forms)

Steps
1. Create both forms with div tag as follows (do not use form tag)

<div id="form_a">
 <input.../>
     <div id="form_b">
        <input.../>
        <input.../>
        <button id="submit_b">Submit B</button>
     </div>
 <input.../>
 <button id="submit_a">Submit A</button>
</div >

2. Add JQuery and Ajax to submit each form data

    <script>
    // Submit form A data
    $('#submit_a').click( function() {
          $.ajax({
            url: 'ajax-url',
            type: 'post',
            dataType: 'json',
            data: $('#form_a input').not( "#form_b input" ).serialize(),
            success: function(data) {
                       // ... do something with the data...
                     }
        });
        });

   // Submit form B data
    $('#submit_b').click( function() {
          $.ajax({
            url: 'ajax-url',
            type: 'post',
            dataType: 'json',
            data: $('#form_b input').serialize(),
            success: function(data) {
                       // ... do something with the data...
                     }
        });
        });
    </script>
Singleton answered 30/11, 2021 at 15:31 Comment(0)
T
-2

If you need your form to submit/commit data to a 1:M relational database, I would recommend creating an "after insert" DB trigger on table A that will insert the necessary data for table B.

Treenware answered 24/9, 2014 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.