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

Q880 How could I make a form with multiple actions?

You are here: irt.org | FAQ | JavaScript | Form | 7.2 | Q880 [ previous next ]

A forms ACTION attribute can be amended in Netscape Navigator 2+ and Microsoft Internet Explorer 4+.

This poses a slight problem. In Netscape Navigator it is simply a case of:

<form name="myForm" action="something">

<script language="JavaSript"><!--
document.myForm.action = 'anything';
//--></script>

However, to avoid this causing problems in Microsoft Internet Explorer 3, then you'll need to do:

<form name="myForm" action="something">

<script language="JavaSript"><!--
if (navigator.appName == 'Netscape')
    document.myForm.action = 'anything';
//--></script>

Or, to support Microsoft Internet Explorer 4, which can amend the ACTION attribute:

<form name="myForm" action="something">

<script language="JavaSript"><!--
if (navigator.appName == 'Netscape' || document.all)
    document.myForm.action = 'anything';
//--></script>

But this doesn't answer the original question. One answer is to actually create two forms with their own different ACTION attibutes, and then use JavaScript to control which form is submitted. If the forms need to share the same form data, then the data needs to be copied between the forms before submission. The following example shows how to use JavaScript to add another form with its own ACTION attribute around an existing HTML form. If JavaScript is not enabled or supported, then only the HTML form will be rendered by the browser:

<script language="JavaScript"><!--
function copyAndSubmit(from,to) {
    to.myText.value = from.myText.value;
    to.submit();
}

document.write('<form name="hiddenForm" action="anything">');
document.write('<input type="hidden" name="myText">');
document.write('<\/form');
//--></script>

<form name="visibleForm" action="something">
<input type="text" name="myText">
<input type="submit" value="Submit something">

<script language="JavaScript"><!--
document.write('<input type="button" value="Submit anything" onClick="copyAndSubmit(document.visibleForm,document.hiddenForm)">');
//--></script>

</form>

Feedback on 'Q880 How could I make a form with multiple actions?'

©2018 Martin Webb