Click here to Skip to main content
15,879,535 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 427K   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

 
QuestionMy vote 5 Pin
Ami_Modi30-Mar-15 19:55
Ami_Modi30-Mar-15 19:55 
Questioneternally useful Pin
Member 89630511-Jul-12 13:18
Member 89630511-Jul-12 13:18 
GeneralMy vote of 5 Pin
Member 816844017-Aug-11 11:09
Member 816844017-Aug-11 11:09 
GeneralOnButton1 command to visit url+formvalue Pin
Member 770283724-Feb-11 6:16
Member 770283724-Feb-11 6:16 
GeneralThanks Pin
beaudetious6-Jan-11 18:12
beaudetious6-Jan-11 18:12 
GeneralForm submit to server Pin
Ajay Kale New18-Oct-10 20:41
Ajay Kale New18-Oct-10 20:41 
Questionis there anyway to send data with this multiple actions on a single form? Pin
alidabiri11-May-08 18:03
alidabiri11-May-08 18:03 
Questionone form allowing one of two actions to occur based on field validation Pin
Qesaf14-Aug-07 15:52
Qesaf14-Aug-07 15:52 
Generalmultiple actions from a single Form: improvement Pin
il_betto7026-Apr-07 5:22
il_betto7026-Apr-07 5:22 
GeneralYet another Thank You Pin
Member 382276413-Feb-07 10:01
Member 382276413-Feb-07 10:01 
GeneralMultiAction with ASP [modified] Pin
Chamark1-Jun-06 9:51
Chamark1-Jun-06 9:51 
GeneralPlease help, related, but not quite the same Pin
skorpiius13-Jan-06 8:47
skorpiius13-Jan-06 8:47 
GeneralLet me restate Pin
skorpiius13-Jan-06 8:54
skorpiius13-Jan-06 8:54 
Generaljavacript version needed. Pin
trix12103-Mar-05 9:49
trix12103-Mar-05 9:49 
GeneralBetween pages Pin
Anonymous18-Nov-04 19:23
Anonymous18-Nov-04 19:23 
GeneralMultiple Actions from one form, another question Pin
Keith Sarver28-Apr-04 5:10
sussKeith Sarver28-Apr-04 5:10 
GeneralRe: Multiple Actions from one form, another question Pin
iamari27-Mar-05 7:35
iamari27-Mar-05 7:35 
i've found one problem with Chris' code, if you have say a hidden field in your form named *action*, you'll get an error saying *object does not support this property or method*
Generalsubmit one message to many message boards Pin
tgopala_krishna26-Jan-04 5:04
tgopala_krishna26-Jan-04 5:04 
General2 Actions 1 form Pin
bheyse15-Jul-03 7:22
bheyse15-Jul-03 7:22 
Questionhow to write dynamically page at a specific location Pin
faad3-Apr-03 22:29
faad3-Apr-03 22:29 
Generalone button Pin
Webgirljudi3-Feb-03 11:29
sussWebgirljudi3-Feb-03 11:29 
GeneralRe: one button Pin
Chris Maunder3-Feb-03 12:01
cofounderChris Maunder3-Feb-03 12:01 
GeneralRe: one button Pin
Anonymous29-Apr-04 15:55
Anonymous29-Apr-04 15:55 
GeneralRe: one button Pin
Hidrogeno11-Apr-08 10:51
Hidrogeno11-Apr-08 10:51 
GeneralThanks Chris, solved another problem of mine! Pin
mwilliamson23-Dec-02 17:38
mwilliamson23-Dec-02 17:38 

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.