开发者

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

开发者 https://www.devze.com 2022-12-11 16:14 出处:网络
I want to have an HTML form embedded in another form like so: <form id=\"form1\"> <in开发者_如何转开发put name=\"val1\"/>

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

<form id="form1">
  <in开发者_如何转开发put 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?


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>


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


quite late but you can do this:

  <form id="form1"></form>
  <form id="form2"></form>
  <input ***form="form1"*** name="val1"/>
  <input ***form="form1"*** name="val2" type="hidden" />

  <input ***form="form2"*** name="val2"/>
  <input ***form="form2"*** type="button" name="Submit Form 2 ONLY">

  <input ***form="form1"*** type="button" name="Submit Form 1 data including form 2" 
         onsubmit="return copyFromForm2Function()">

The "form" element within the input tag has been added to get around the inability to nest forms.


A possible solution : Instead of having the nested form, add an onClick event to the form2 button which will call a JS method that could get your specific items (val2 input in this case) from form1 and using AJAX or simply xmlHTTPRequests() to perform the desired POST methods.


As other people have said, you cannot nest form elements. The way I would handle something like this would be to use a single form and then group the elements with fieldsets. You can then add events to the submit buttons with javascript and enable/disable the input fields that should be submitted.

With jQuery, MooTools or any other framework this would be very simple. It will break if the client disables scripts, though.

A MooTools solution could look like this:

$('submit2').addEvent('click', function (e) {
    e.stop();
    $$('#fieldset1 input').set('disabled', 'disabled');
    $('form').submit();
    $$('#fieldset2 input').set('disabled', '');
}

Oh, and I trust you have a good reason for doing this, because to me it sounds suspiciously like bad usability :-)


I think there may be issues with the UI for this. It'd be very confusing for a user if only part of (what appears to be) a single form was submitted/saved.

Rather than nesting forms, which, as stated, is invalid, I think you need to look at perhaps implementing some AJAX calls instead to update subset of data.


Here is the definitive working answer. I didn't need to create an extra parent DIV and name it id="place_here". Naming a table cell id="place_here" and making it the parent to DIV id="div_2" was enough. This is a brilliant little work around. Someone on another thread helped me with this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"><head>
<title>test / crtp</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">
$(document).ready(function () {
    position_mdiv()();
    $(window).resize(function() {
        position_mdiv();
    });
})

function position_mdiv(){

    var pos = $('#place_here').position();
    var width = $('#place_here').outerWidth();

    $('#div_2').css({
    position: "absolute",
    top: pos.top +2 + "px",
    left: (pos.left -300 + width) + "px"
});

}

</script>
<body>

<form id="CTRP_Form">
<table border="1">
<tr>
<td>
<div id="div_1"><input id="fnam" name="fnam" form="CTRP_Form" type="text"><input type=submit></div>
</td>
<td id="place_here" style="background:yellow;width:300px;padding:0px;border:solid 2px #CCC"></td>
</tr>
</table>
</form>


<div id="div_2"><form id="query_Form"><input id="MyQuery" name="MyQuery" form="query_Form" type="text"><input type=submit></form></div>

</body>
</html>


I resolved this by having multiple submit buttons in the form. The buttons reference different CGIs and brought along the additional fields that I needed to handle conditional processing in the CGIs.

Code snippet

<form name="ep" method="put" action="/cgi-bin/edit_plan.pl">
   [...] 
   <tr>
      <td><input type="text" size="20" value="red" name="RN0"></td>
      <td><input type="text" size="3" value="2" name="RT0"></td>
      <td><input type="submit" value="Set row 0" name="RUN0"></td>
   </tr>
   [...] Add as many rows as needed, increment the 0 in all places Add an ending submit for overall processing instead of row processing: <input type="submit" value="Set ALL" name="SET"> 
</form>


It's not valid and will in my experience produce arbitrary results.

You could work around this using Javascript's DOM functions, taking form2 out of form1, putting it into the body, submitting it and putting it back into form1.

Edit: This won't be 100% right either, as it still has nested forms. As some of the answers point out, you have to have two separate forms. You can still do this using DOM magic but with a few more turns - .see Randolpho's answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消