Click here to Skip to main content
15,885,067 members
Articles / Programming Languages / Javascript
Article

Specifying multiple actions from a single Form

Rate me:
Please Sign up or sign in to vote.
4.88/5 (14 votes)
1 Aug 2000CPOL 427.2K   1K   21   54
A simple way to send the data from a single form to different pages, depending on which 'submit' button the user chooses

Introduction

In writing the Self-posting scripts I needed a way to allow the user to send their HTML to either the next page for processing, or to a preview page so they could check first. Basically I wanted 2 "submit" buttons on a single form, but there seemed no obvious way to do it. After asking around and (gasp!) reading the docs I came across a simple method that I hope will save someone else half a day of messing around.

The Problem

Say I have a form such as below:

Screenshot - form.png

We want Button1 to send the data to one page, and Button2 to send it to a completely different page. Your <form> tag specifies the action to take when the submit button is pressed - but we can only specify a single action.

A Solution

One way to get around this is to handle each button's OnClick event and set the "action" for the form dynamically:

ASP.NET
<!-- create the form -->
<form name="Form1" method="post">

<!-- Add the data entry bits -->
Your Name <input type="text" name="text1" size="10" /><br />

<!-- Add some buttons -->
<INPUT type="button" value="Button1" name=button1 onclick="return OnButton1();">
<INPUT type="button" value="Button2" name=button2 onclick="return OnButton2();">

<!-- close the form -->
</form>

Our button click handlers for Button1 and Button2 would look like the following:

ASP.NET
<script language="Javascript">
<!--
function OnButton1()
{
    document.Form1.action = "Page1.aspx"
    document.Form1.target = "_blank";    // Open in a new window
    document.Form1.submit();             // Submit the page
    return true;
}

function OnButton2()
{
    document.Form1.action = "Page2.aspx"
    document.Form1.target = "_blank";    // Open in a new window
    document.Form1.submit();             // Submit the page
    return true;
}
-->
</script>
<noscript>You need Javascript enabled for this to work</noscript>

Where Page1.aspx should be called when Button1 is pressed, and Page2.aspx called when Button2 is pressed.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
GeneralRe: two actions from the same form Pin
Anglo31-Oct-02 5:21
Anglo31-Oct-02 5:21 
GeneralTwo actions in one form Pin
17-Apr-02 23:11
suss17-Apr-02 23:11 
Generalrearrage field order Pin
10-Dec-01 16:07
suss10-Dec-01 16:07 
GeneralJava Function call Pin
6-Aug-01 9:44
suss6-Aug-01 9:44 
GeneralRe: Java Function call Pin
6-Aug-01 9:50
suss6-Aug-01 9:50 
Generaldifferent size windows Pin
sac3-Aug-01 10:13
sac3-Aug-01 10:13 
GeneralRe: different size windows Pin
28-Feb-02 4:50
suss28-Feb-02 4:50 
GeneralRe: different size windows Pin
Anonymous23-Sep-02 1:17
Anonymous23-Sep-02 1:17 
I've got it to work with a pop up by opening the pop window first, then you use the target= attibute to submit the form to the new pop up window: -

//Function to open pop up window
function openWin(theURL,winName,features) {
window.open(theURL,winName,features);
}


//Function to open preview post window
function OpenPreviewWindow(){

now = new Date

//Open the window first
openWin('post_preview.asp','preview','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width=680,height=400')

//Now submit form to the new window
document.frmAddMessage.action = "post_preview.asp?ID=" + now.getTime();
document.frmAddMessage.target = "preview";
document.frmAddMessage.submit();

return true;
}


Seems to work in IE and Netscrap 4 and the date part in the querystring stops the browser caching the page.
GeneralMr Picky!! [modified] Pin
26-Jul-01 5:19
suss26-Jul-01 5:19 
GeneralRe: Mr Picky-Bag !! Pin
Pedro Miranda13-Sep-02 5:40
Pedro Miranda13-Sep-02 5:40 
Generalbrowser compatiblity issues Pin
2-May-01 15:07
suss2-May-01 15:07 
GeneralRe: browser compatiblity issues Pin
asthala vistha22-Jul-05 21:11
sussasthala vistha22-Jul-05 21:11 
Generaluploading Pin
4-Mar-01 22:20
suss4-Mar-01 22:20 
GeneralBrowser issues Pin
Brian V. Shifrin3-Aug-00 2:59
sussBrian V. Shifrin3-Aug-00 2:59 
GeneralRe: Browser issues Pin
Chris Maunder3-Aug-00 12:27
cofounderChris Maunder3-Aug-00 12:27 
GeneralRe: Browser issues Pin
Anonymous6-Aug-02 17:36
Anonymous6-Aug-02 17:36 
GeneralRe: Browser issues Pin
Domenic Denicola6-Aug-02 17:36
Domenic Denicola6-Aug-02 17:36 
GeneralTwo or more SUBMIT buttons Pin
Tom Wellige2-Aug-00 21:13
Tom Wellige2-Aug-00 21:13 
GeneralRe: Two or more SUBMIT buttons Pin
Chris Maunder3-Aug-00 2:46
cofounderChris Maunder3-Aug-00 2:46 
GeneralRe: Two or more SUBMIT buttons Pin
11-Feb-01 23:59
suss11-Feb-01 23:59 
GeneralRe: Two or more SUBMIT buttons Pin
Tom Wellige12-Feb-01 0:54
Tom Wellige12-Feb-01 0:54 
GeneralRe: Two or more SUBMIT buttons Pin
12-Feb-01 23:40
suss12-Feb-01 23:40 
GeneralRe: Two or more SUBMIT buttons Pin
23-Feb-01 18:42
suss23-Feb-01 18:42 
GeneralRe: Two or more SUBMIT buttons Pin
16-Dec-01 14:25
suss16-Dec-01 14:25 
GeneralRe: Two or more SUBMIT buttons Pin
16-Dec-01 14:27
suss16-Dec-01 14:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.