This is a mirror of official site: http://jasper-net.blogspot.com/

How to dispose multiple submit buttons on a single form

| Thursday, December 2, 2010
Introduction

Sometimes, you could be forced to send same data on a single form to different URL , or you want to use multiple buttons to  handle different requests.Using multiple submit buttons (with same name and same type) on a single form is  extremely available.However, how do you check which buttons was pressed on Server and Client? This article shows you how to do it.

Background  

On Server, I take VBScript for example.  I use Request.Form("bufftonname") Through  getting different button value to distinguish pressed button.

On Client,  no doubt, I use javacript.  Throuth value of document.pressed , I can differ which button pressed.  

Sample  

At First, my HTML code like this:  

<form method="post" >
   <input id="Text1" name="Text1" type="text" />
   <br />
   <input name="mybutton" type="submit" value="button1" />
   <input name="mybutton" type="submit" value="button2" />
</form>  
This is a very simple program, only with one form which contains one text input and two submit buttons which have same name--"mybutton", same type--"submit" and different value--"button1" and "button2". when we create script, these unique values will help us to check which button was pressed.

Now, I add some VBScript to  respose the different button request.

<%
         if request.form("mybutton") ="button1" then
             response.Write "Oh, you pressed button 1"
         elseif request.form("mybutton") = "button2" then
             response.Write "Aha, you pressed button 2"
         end if
%>

Above is just  code which I want to display on SERVER. The idea is not complicated, and the code is simple.

Until now, my text input can't  work. So I increase some codes to check if my input is null, and chenge my SERVER scripts to display it's value only by pressing button1( this means I hope button2 stay the same ).

    <%
         if request.form("mybutton") ="button1" then
             response.Write "Oh, you pressed button 1, now Text1 text show:"&request.Form("Text1")
         elseif request.form("mybutton") = "button2" then
             response.Write "Aha, you pressed button 2"
         end if
    %>
   <form method="post" onsubmit="return form_check();" >
       <input id="Text1" name="Text1" type="text" />
       <br />
       <input name="mybutton" type="submit" value="button1" />

Read more: Codeproject

Posted via email from .NET Info

0 comments: