5,696,038 members and growing! (12,469 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate License: The Code Project Open License (CPOL)

Specifying multiple actions from a single Form

By Chris Maunder

A simple way to send the data from a single form to different pages, depending on which 'submit' button the user chooses
Javascript, Windows, Visual Studio, Dev

Posted: 1 Aug 2000
Updated: 1 Aug 2000
Views: 194,851
Bookmarked: 16 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
27 votes for this Article.
Popularity: 6.56 Rating: 4.58 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
2 votes, 16.7%
4
10 votes, 83.3%
5

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:

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

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

About the Author

Chris Maunder


Sitebuilder, Financialadmin, Editor, Staff, Admin
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.
Occupation: Founder
Company: The Code Project
Location: Canada Canada

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 48 (Total in Forum: 48) (Refresh)FirstPrevNext
Generalis there anyway to send data with this multiple actions on a single form?memberalidabiri19:03 11 May '08  
Questionone form allowing one of two actions to occur based on field validationmemberQesaf16:52 14 Aug '07  
Generalmultiple actions from a single Form: improvementmemberil_betto706:22 26 Apr '07  
GeneralYet another Thank Youmember11:01 13 Feb '07  
GeneralMultiAction with ASP [modified]memberChamark10:51 1 Jun '06  
GeneralPlease help, related, but not quite the samememberskorpiius9:47 13 Jan '06  
GeneralLet me restatememberskorpiius9:54 13 Jan '06  
Generaljavacript version needed.membertrix121010:49 3 Mar '05  
GeneralBetween pagessussAnonymous20:23 18 Nov '04  
GeneralMultiple Actions from one form, another questionsussKeith Sarver6:10 28 Apr '04  
GeneralRe: Multiple Actions from one form, another questionmemberiamari8:35 27 Mar '05  
Generalsubmit one message to many message boardsmembertgopala_krishna6:04 26 Jan '04  
General2 Actions 1 formsussBrad Heyse8:22 15 Jul '03  
Generalhow to write dynamically page at a specific locationmemberfaad23:29 3 Apr '03  
Generalone buttonsussWebgirljudi12:29 3 Feb '03  
GeneralRe: one buttonadminChris Maunder13:01 3 Feb '03  
GeneralRe: one buttonsussAnonymous16:55 29 Apr '04  
GeneralRe: one buttonmemberHidrogeno11:51 11 Apr '08  
GeneralThanks Chris, solved another problem of mine!membermwilliamson18:38 23 Dec '02  
GeneralThanks ChrismemberNick Parker5:27 30 Jul '02  
Generaltwo actions from the same formsussAnonymous3:50 24 Jul '02  
GeneralRe: two actions from the same formsussAnonymous18:33 6 Aug '02  
GeneralRe: two actions from the same formmemberAnglo6:21 31 Oct '02  
GeneralTwo actions in one formmemberBetty0:11 18 Apr '02  
Generalrearrage field ordermemberAlicia17:07 10 Dec '01  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Aug 2000
Editor: Chris Maunder
Copyright 2000 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project