(page requirements)

Using multiple submit buttons on a single form

Wondering how to create an HTML form with more than a single submit button and check which of those buttons was pressed?
 
First, let's create a form with a single submit button called "Submit 1" (assuming that the script we're calling to handle the form submission or the ACTION attribute is "mulsub.asp").
 
<FORM action="mulsub.asp" method="post">
First name: <INPUT type="TEXT" name="FNAME"><BR>
<INPUT type="submit" name="bsubmit" value="Submit 1">
</FORM>
Listing #1 : HTML code. Download onesub.htm (0.26 KB).
 
Now let's add two more submit buttons to the form and call them "Submit 2" and "Submit 3."
 
<FORM action="mulsub.asp" method="post">
First name: <INPUT type="TEXT" name="FNAME"><BR>
<INPUT type="submit" name="bsubmit" value="Submit 1">

<INPUT type="submit" name="bsubmit" value="Submit 2">
<INPUT type="submit" name="bsubmit" value="Submit 3">
</FORM>
Listing #2 : HTML code. Download mulsub.htm (0.27 KB).
 
Note how we kept the name of all three submit buttons the same -- "bsubmit." The only difference is the "value" attribute with unique values -- "Submit 2" and "Submit 3." When we create the script, these unique values will help us to figure out which of the submit buttons was pressed.
 
Following is a sample Active Server Pages script to check for the submit button. We're simply checking the value of the button(s) named "bsubmit" and performing the appropriate action within the script.
 
<HTML><BODY>
<%
  btnID = "?"

  if Request.Form("bsubmit") = "Submit 1" then
    btnID = "1"
  elseif Request.Form("bsubmit") = "Submit 2" then
    btnID = "2"
  elseif Request.Form("bsubmit") = "Submit 3" then
    btnID = "3"
  end if
%>
Submit button ID: <%=btnID%>
</BODY></HTML>
Listing #3 : ASP/VBScript code. Download mulsub0.asp (0.28 KB).
 
Finally let's combine the HTML form tags and the script commands into a single ASP script.
 
<HTML>
<BODY>

<%
  btnID = "?"

  if Request.Form("bsubmit") = "Submit 1" then
    btnID = "1"
  elseif Request.Form("bsubmit") = "Submit 2" then
    btnID = "2"
  elseif Request.Form("bsubmit") = "Submit 3" then
    btnID = "3"
  end if
%>
Submit button ID: <%=btnID%><BR>

<FORM action="mulsub.asp" method="post">
First name: <INPUT type="TEXT" name="FNAME"><BR>
<INPUT type="submit" name="bsubmit" value="Submit 1">
<INPUT type="submit" name="bsubmit" value="Submit 2">
<INPUT type="submit" name="bsubmit" value="Submit 3">
</FORM>

</BODY>
</HTML>
Listing #4 : ASP/VBScript code. Download mulsub.asp (0.38 KB).
 
 
Applicable Keywords : Active Server Pages, HTML, HTML 3.2, HTML 4.0, Internet Explorer, Explorer 2.x, Explorer 3.x, Explorer 4.x, Explorer 5.x, Internet, Netscape Navigator, Navigator 2.x, Navigator 3.x, Communicator 4.x, Netscape 6.x, World Wide Web
 
 
 
Copyright © 2009 Chami.com. All Rights Reserved. | Advertise | Created in HTML Kit editor