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.
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 intFormID = FormSubmittedName () 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 function FormSubmittedName () dim strFormButton1, strFormButton2, strFormButton3 strFormButton1 = request.Form("SubmitButton1") strFormButton2 = request.Form("SubmitButton2") strFormButton3 = request.Form("SubmitButton3") if strFormButton1 <> "" then FormSubmittedName = 1 elseif strFormButton2 <> "" then FormSubmittedName = 2 elseif strFormButton3 <> "" then FormSubmittedName = 3 end if end function%>