Click here to Skip to main content
11,935,031 members (51,323 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

Specifying multiple actions from a single Form

, 1 Aug 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple way to send the data from a single form to different pages, depending on which 'submit' button the user chooses


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:

<!-- 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 -->

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

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

function OnButton2()
    document.Form1.action = "Page2.aspx" = "_blank";    // Open in a new window
    document.Form1.submit();             // Submit the page
    return true;
<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.


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


About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...

Comments and Discussions

QuestionMy vote 5 Pin
Ami_Modi30-Mar-15 20:55
memberAmi_Modi30-Mar-15 20:55 
Questioneternally useful Pin
Member 89630511-Jul-12 14:18
memberMember 89630511-Jul-12 14:18 
GeneralMy vote of 5 Pin
Member 816844017-Aug-11 12:09
memberMember 816844017-Aug-11 12:09 
GeneralOnButton1 command to visit url+formvalue Pin
Member 770283724-Feb-11 7:16
memberMember 770283724-Feb-11 7:16 
GeneralThanks Pin
beaudetious6-Jan-11 19:12
memberbeaudetious6-Jan-11 19:12 
GeneralForm submit to server Pin
Ajay Kale New18-Oct-10 21:41
memberAjay Kale New18-Oct-10 21:41 
Questionis there anyway to send data with this multiple actions on a single form? Pin
alidabiri11-May-08 19:03
memberalidabiri11-May-08 19:03 
Questionone form allowing one of two actions to occur based on field validation Pin
Qesaf14-Aug-07 16:52
memberQesaf14-Aug-07 16:52 
Generalmultiple actions from a single Form: improvement Pin
il_betto7026-Apr-07 6:22
memberil_betto7026-Apr-07 6:22 
GeneralYet another Thank You Pin
Member #382276413-Feb-07 11:01
memberMember #382276413-Feb-07 11:01 
GeneralMultiAction with ASP [modified] Pin
Chamark1-Jun-06 10:51
memberChamark1-Jun-06 10:51 
GeneralPlease help, related, but not quite the same Pin
skorpiius13-Jan-06 9:47
memberskorpiius13-Jan-06 9:47 
GeneralLet me restate Pin
skorpiius13-Jan-06 9:54
memberskorpiius13-Jan-06 9:54 
Generaljavacript version needed. Pin
trix12103-Mar-05 10:49
membertrix12103-Mar-05 10:49 
GeneralBetween pages Pin
Anonymous18-Nov-04 20:23
sussAnonymous18-Nov-04 20:23 
GeneralMultiple Actions from one form, another question Pin
Keith Sarver28-Apr-04 6:10
sussKeith Sarver28-Apr-04 6:10 
GeneralRe: Multiple Actions from one form, another question Pin
iamari27-Mar-05 8:35
memberiamari27-Mar-05 8:35 
Generalsubmit one message to many message boards Pin
tgopala_krishna26-Jan-04 6:04
membertgopala_krishna26-Jan-04 6:04 
General2 Actions 1 form Pin
Brad Heyse15-Jul-03 8:22
sussBrad Heyse15-Jul-03 8:22 
Questionhow to write dynamically page at a specific location Pin
faad3-Apr-03 23:29
memberfaad3-Apr-03 23:29 
Generalone button Pin
Webgirljudi3-Feb-03 12:29
sussWebgirljudi3-Feb-03 12:29 
GeneralRe: one button Pin
Chris Maunder3-Feb-03 13:01
adminChris Maunder3-Feb-03 13:01 
GeneralRe: one button Pin
Anonymous29-Apr-04 16:55
sussAnonymous29-Apr-04 16:55 
GeneralRe: one button Pin
Hidrogeno11-Apr-08 11:51
memberHidrogeno11-Apr-08 11:51 
GeneralThanks Chris, solved another problem of mine! Pin
mwilliamson23-Dec-02 18:38
membermwilliamson23-Dec-02 18:38 
GeneralThanks Chris Pin
Nick Parker30-Jul-02 5:27
memberNick Parker30-Jul-02 5:27 
Generaltwo actions from the same form Pin
Anonymous24-Jul-02 3:50
sussAnonymous24-Jul-02 3:50 
GeneralRe: two actions from the same form Pin
Anonymous6-Aug-02 18:33
sussAnonymous6-Aug-02 18:33 
GeneralRe: two actions from the same form Pin
Anglo31-Oct-02 6:21
memberAnglo31-Oct-02 6:21 
GeneralTwo actions in one form Pin
Betty18-Apr-02 0:11
memberBetty18-Apr-02 0:11 
Generalrearrage field order Pin
Alicia10-Dec-01 17:07
memberAlicia10-Dec-01 17:07 
GeneralJava Function call Pin
Sonny Ferguson6-Aug-01 10:44
memberSonny Ferguson6-Aug-01 10:44 
GeneralRe: Java Function call Pin
Sonny Ferguson6-Aug-01 10:50
memberSonny Ferguson6-Aug-01 10:50 
Generaldifferent size windows Pin
sac3-Aug-01 11:13
membersac3-Aug-01 11:13 
GeneralRe: different size windows Pin
Anonymous28-Feb-02 5:50
memberAnonymous28-Feb-02 5:50 
GeneralRe: different size windows Pin
Anonymous23-Sep-02 2:17
sussAnonymous23-Sep-02 2:17 
GeneralMr Picky!! [modified] Pin
Anonymous26-Jul-01 6:19
memberAnonymous26-Jul-01 6:19 
GeneralRe: Mr Picky-Bag !! Pin
13-Sep-02 6:40
suss13-Sep-02 6:40 
Generalbrowser compatiblity issues Pin
Lenore Snell2-May-01 16:07
memberLenore Snell2-May-01 16:07 
GeneralRe: browser compatiblity issues Pin
asthala vistha22-Jul-05 22:11
sussasthala vistha22-Jul-05 22:11 
Generaluploading Pin
Anonymous4-Mar-01 23:20
memberAnonymous4-Mar-01 23:20 
GeneralBrowser issues Pin
Brian V. Shifrin3-Aug-00 3:59
sussBrian V. Shifrin3-Aug-00 3:59 
GeneralRe: Browser issues Pin
Chris Maunder3-Aug-00 13:27
sussChris Maunder3-Aug-00 13:27 
GeneralRe: Browser issues Pin
Anonymous6-Aug-02 18:36
sussAnonymous6-Aug-02 18:36 
GeneralRe: Browser issues Pin
Domenic [Geekn]6-Aug-02 18:36
memberDomenic [Geekn]6-Aug-02 18:36 
GeneralTwo or more SUBMIT buttons Pin
Tom Wellige2-Aug-00 22:13
sussTom Wellige2-Aug-00 22:13 
GeneralRe: Two or more SUBMIT buttons Pin
Chris Maunder3-Aug-00 3:46
sussChris Maunder3-Aug-00 3:46 
GeneralRe: Two or more SUBMIT buttons Pin
dr0n9912-Feb-01 0:59
memberdr0n9912-Feb-01 0:59 
GeneralRe: Two or more SUBMIT buttons Pin
Tom Wellige12-Feb-01 1:54
memberTom Wellige12-Feb-01 1:54 
GeneralRe: Two or more SUBMIT buttons Pin
dr0n9913-Feb-01 0:40
memberdr0n9913-Feb-01 0:40 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 2 Aug 2000
Article Copyright 2000 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid