Saturday, September 1, 2007

Creating and handling a form with multiple submit buttons with images

Wondering how to create an HTML form with more than a single submit button with images and check which of those buttons was clicked?

First, let's take a look at the HTML code required to make a form with multiple "submit" image buttons. Note how each of the "image" type input buttons have unique names. We will be using these names to find out which of the buttons was pressed. The SRC attribute should point to unique image files, although technically you could use the same image file for all three of those input buttons. We're assuming that the script we're calling to handle the form submission (or the ACTION attribute) is "mulibtn.asp" which we will demonstrate next.




First name:







Listing #1 : HTML code. Download mulibtn.htm (0.3 KB).

Following is a sample Active Server Pages (ASP) script that you can use in conjunction with the above form. When an image button is clicked, the script will receive the position of the mouse pointer as X, Y coordinates relative to the clicked image. For example, the coordinates of the button "bsubmit1" will be sent to the script as "bsubmit1.x" and "bsubmit1.y" If a button other than "bsubmit1" was clicked, the coordinates for "bsubmit1" will contain an empty value (Nil, Empty, NULL or "" value depending on the scripting language you're using). In our ASP sample which is using VBScript, we'll check for the "nil" value.



<%
btnID = "?"

if Request.Form("bsubmit1.x") <> nil then
btnID = "1"
elseif Request.Form("bsubmit2.x") <> nil then
btnID = "2"
elseif Request.Form("bsubmit3.x") <> nil then
btnID = "3"
end if
%>
Submit button ID: <%=btnID%>



Listing #2 : ASP/VBScript code. Download mulibtn0.asp (0.29 KB).

Finally we'll combine the HTML code and the ASP script code into a single file as follows:



<%
btnID = "?"

if Request.Form("bsubmit1.x") <> nil then
btnID = "1"
elseif Request.Form("bsubmit2.x") <> nil then
btnID = "2"
elseif Request.Form("bsubmit3.x") <> nil then
btnID = "3"
end if
%>
Submit button ID: <%=btnID%>



First name:







Listing #3 : ASP/VBScript code. Download mulibtn.asp (0.39 KB).

By Chami.com

No comments: