Scripting Master Logo
Learn Scripting Languages from the Master!

Determine form submitted

Home : Scripts > Scripts for ASP > Determine form submitted

In cases where you have multiple forms on the same web page, there is a need to determine which form the user submitted. The code below first uses HTML to create three forms and then we use ASP code to determine which form the user submits.

So how can our ASP script determine which form the user submitted? It is simple: we assign a unique value to the submit button in each form with HTML and then when we process the form with ASP, we read the value of the submit button. Thus this identifies which form the user submitted when the script reads which button the user clicked on.

The output of the script:

Click on a submit button below to submit a form and the script will determine the form you submitted.

Form 1


Form 2


Form 3


Scripting code:

Step 1: create three forms and assign a unique name to each of the submit button in the form:

<form action="determine-form-submitted.asp" method="post">
<h6>Form 1</h6>
<textarea rows="2" cols="30" name="textarea1">Form 1.</textarea>
<input type="submit" value="Submit Form 1" name="SubmitButton1" />
<hr />
</form>
<form action="determine-form-submitted.asp" method="post">
<h6>Form 2</h6>
<textarea rows="2" cols="30" name="textarea2">Form 2.</textarea>
<input type="submit" value="Submit Form 2" name="SubmitButton2" />
<hr />
</form>
<form action="determine-form-submitted.asp" method="post">
<h6>Form 3</h6>
<textarea rows="2" cols="30" name="textarea3">Form 3.</textarea>
<input type="submit" value="Submit Form 3" name="SubmitButton3" />
<hr />
</form>

Step 2: use ASP to determine which form was submitted. For this code to work, note that this script must be placed in a filed saved as "determine-form-submitted.asp" because this is the file name we specified above to process our forms:

<%
dim intFormID, shtml
' Call the FormSubmittedName () function to determine which form is submitted
intFormID = FormSubmittedName ()
' display message if a form is submitted
select case intFormID
case 1, 2, 3
shtml = "<p>You submitted form # <strong>" & intFormID & "</strong>"
shtml = shtml & " and the text <strong>"
shtml = shtml & request.form("textarea" & intFormID) & "</strong></p>"
response.write shtml
response.write "<hr />"
end select

' this function determines which form is submitted
function FormSubmittedName ()
dim strFormButton1, strFormButton2, strFormButton3
' get the values of each of the submit buttons
strFormButton1 = request.Form("SubmitButton1")
strFormButton2 = request.Form("SubmitButton2")
strFormButton3 = request.Form("SubmitButton3")
' now determine which form is submitted by checking the value each
' of the strButton variable holds. If the variable contains a value,
' it means that a form is submitted; otherwise, a form is not submitted
if strFormButton1 <> "" then
' form 1 is submitted
FormSubmittedName = 1
elseif strFormButton2 <> "" then
' form 2 is submitted
FormSubmittedName = 2
elseif strFormButton3 <> "" then
' form 3 is submitted
FormSubmittedName = 3
end if
end function
%>