Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q190 How do you combine two forms before submitting them as one form?

You are here: irt.org | FAQ | JavaScript | Form | 10.2 | Q190 [ previous next ]

In one of the forms, or perhaps in a third form, have hidden form fields which you can then copy the contents of the visible form fields to and then submit that form.

You'll need to trap the users form submission and cancel it, and then you'll need to copy the data, and then you'll need to submit the require form:

<SCRIPT LANGUAGE="JavaScript"><!--
function copydata() {
    document.formthree.mytextone.value = document.formone.mytextone.value;
    document.formthree.mytexttwo.value = document.formtwo.mytexttwo.value;
    document.formthree.submit();
    return false;
}
//--></SCRIPT>

<FORM NAME="formone" onSubmit="return copydata()">
Enter some text: <INPUT TYPE="text" NAME="mytextone">
<P><INPUT TYPE="BUTTON" VALUE="Submit" onClick="copydata()">
</FORM>

<FORM NAME="formtwo" onSubmit="return copydata()">
Enter some text: <INPUT TYPE="text" NAME="mytexttwo">
<P><INPUT TYPE="BUTTON" VALUE="Submit" onClick="copydata()">
</FORM>

<FORM NAME="formthree">
<INPUT TYPE="HIDDEN" NAME="mytextone">
<INPUT TYPE="HIDDEN" NAME="mytexttwo">
</FORM>

Note, if the action of the third form is mailto: then it'll fail without telling you (a security hobble). Any onSubmit event handler of the third form will not be invoked, so make sure you perform your form validation before the line document.formthree.submit()

Feedback on 'Q190 How do you combine two forms before submitting them as one form?'

©2018 Martin Webb