Click here to Skip to main content
15,891,981 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.4K   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: 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 
Thanks Tom. My original problem was that I need to send the data from the form to two completely separate pages (one was a preview that could be kept open at all time, and the other was a processing page. Using multiple submit buttons is certainly a nicer way of doing it when you can use the same window for both options.

cheers
Chri
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 
GeneralRe: Two or more SUBMIT buttons Pin
6-Mar-02 9:46
suss6-Mar-02 9:46 

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.