Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed an HTML <form> within a larger <form>?

Tags:

html

I want to have an HTML form embedded in another form like so:

<form id="form1">   <input name="val1"/>   <form id="form2">     <input name="val2"/>     <input type="button" name="Submit Form 2 ONLY">   </form> <input type="button" name="Submit Form 1 data including form 2"> </form> 

I need to submit the entirety of form1, but when I submit form2 I only want to submit the data in form2 (not everything in form1.) Will this work?

like image 408
MikeN Avatar asked Nov 18 '09 20:11

MikeN


People also ask

Can you put a form within a form 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."

Can you put a form inside a form?

You can use plenty of SUBMIT buttons inside a single form, but you can't manage a nested form appropriately.

Can I use form tag inside form tag?

No, nested forms are forbidden. This means A FORM has a mandatory start tag, mandatory end tag and can contain anything in %block or SCRIPT, except other FORMs.


2 Answers

What you have described will not work.

One workaround would be to create two forms that are not nested. You would use hidden inputs for your original parent form that duplicate the inputs from your original nested form. Then use Javascript/DOM manipulation to hook the submit event on your "parent" form, copying the values from the "nested" form into the hidden inputs in the "parent" form before allowing the form to submit.

Your form structure would look something like this (ignoring layout HTML):

<form id="form1">   <input name="val1"/>   <input name="val2" type="hidden" />   <input type="button" name="Submit Form 1 data including form 2"           onsubmit="return copyFromForm2Function()"> </form> <form id="form2">   <input name="val2"/>   <input type="button" name="Submit Form 2 ONLY"> </form> 
like image 173
Randolpho Avatar answered Sep 21 '22 11:09

Randolpho


You cannot have nested forms (source) so this will not work.

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

like image 27
Marek Karbarz Avatar answered Sep 23 '22 11:09

Marek Karbarz