Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call a sub when user clicks submit button in form

Status
Not open for further replies.

dommett

Programmer
Apr 30, 2004
33
US
How do you call a sub (within the same file as the form) when the user clicks the submit button? I have a asp file written with HTML and VBscript that I want to use for adding, editing, or deleting a record set of my access database. I want this to all be done within one asp file so I don't have to have six smaller files (form for adding, file to complete the adding, form for editing, file to complete editing, etc.)

Is this possible? I have all the code written but I just don't know how to get the submit button to call my sub. I can provide the code if anyone wants to see what I'm talking about.

I'm a newbie here so you won't insult me with idiot instructions!
Thanks,
Amy
 
I assume you mean an ASP sub? You can't call ASP Subs or Functions directly from the HTML Form since the ASP is all run on the server.

What you can do though is to submit your FORM with the button and pass a value to the ACTION page telling it what Sub or Function to carry out.

Post the code you already have and we'll see how it can be modified to suit your needs.

Tony
________________________________________________________________________________
 
Here's what I currently have:

<html>

<head>
</head>
<%
Dim adoCon
Dim rsCoupon
Dim strSQL
Dim txtActionType
Dim lngID

Sub cmdTakeAction()
'for testing purposes - I just want to see the
'redirection work
Response.Redirect "Default.htm"
' Select Case txtActionType
' Case "Add","Edit"
' rsCoupon.Fields("Company Name") = FrontPage_Form1("CompanyName")
' rsCoupon.Fields("Coupon Description") = FrontPage_Form1("CouponDescription")
' rsCoupon.Fields("Hyperlink to JPEG") = FrontPage_Form1("HyperlinkToJPEG")
' rsCoupon.Fields("Start Date") = FrontPage_Form1("StartDate")
' rsCoupon.Fields("End Date") = FrontPage_Form1("EndDate")
' rsCoupon.Update
' Case "Delete"
' strSQL = "DELETE FROM Main WHERE Main.ID=" & lngID
' adoCon.Execute(strSQL)
' End Select
' rsCoupon.Close
' Set rsCoupon = Nothing
' Set adoCon = Nothing
End Sub

txtActionType = Request.QueryString("Action")
lngID = Request.QueryString("ID")

'Open connection

Set adoCon = Server.CreateObject("ADODB.Connection")

adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="& Server.MapPath("../fpdb/testcoupon.mdb")

Set rsCoupon = Server.CreateObject("ADODB.Recordset")

if txtActionType = "Add" then
strSQL = "SELECT Main.* FROM Main;"
rsCoupon.CursorType = 2
rsCoupon.LockType = 3
else 'txtActionType is Delete or Edit
strSQL = "SELECT Main.* FROM Main WHERE Main.ID=" & lngID
end if

rsCoupon.Open strSQL, adoCon

if txtActionType = "Add" then
rsCoupon.AddNew
rsCoupon("Start Date") = Date()
rsCoupon("Expiration Date") = DateSerial(Year(Date), Month(Date) + 1, Day(Date)) 'add a month to today
rsCoupon("Hyperlink to JPEG") = "ecoupons/"
end if
%>


<body onload="document.FrontPage_Form1.CompanyName.focus()"><form method="POST" name="FrontPage_Form1">
<h3 align="center"><% Response.Write(txtActionType) & " Coupon" %></h3>
<p align="center">Company Name
<input type="text" name="CompanyName" size="60" value = '<%Response.Write(rsCoupon("Company Name"))%>' tabindex="1"></p>
<p align="center">Coupon Description
<input type="text" name="CouponDescription" size="70" value = '<%Response.Write(rsCoupon("Coupon Description"))%>' tabindex="2"></p>
<p align="center">Hyperlink to JPEG
<!--webbot bot="Validation" s-display-name="Hyperlink to JPEG" s-data-type="String" b-allow-letters="TRUE" b-allow-digits="TRUE" s-allow-other-chars="/" --><input type="text" name="HyperlinkToJPEG" size="50" value=<%Response.Write(rsCoupon("Hyperlink to JPEG"))%> tabindex="3"></p>
<p align="center">Start Date
<input type="text" name="StartDate" size="10" value = '<%Response.Write(rsCoupon("Start Date"))%>' tabindex="4"></p>
<p align="center">Expiration Date
<input type="text" name="ExpirationDate" size="10" value = '<%Response.Write(rsCoupon("Expiration Date"))%>' tabindex="5"></p>
<p align="center">
<input type="submit" value="Submit" name="B1" tabindex="6" onclick="cmdTakeAction"></a><input type="reset" value="Reset" name="B2"></p>
</p>
</form>

<p align="center"></body>

</html>

Thank you!
Amy
 
For starters, onClick="cmdTakeAction" is a client-side event (onClick) trying to call a server-side subroutine (cmsTakeAction). This won't work I'm afraid.

1) Take the code out of Sub cmdTakeAction
2) add this to your FORM tag : ACTION="myPage.asp" where mypage.asp is the name of this file.
3) remove the onClick event of the Submit button

Also - where do you decide on the ActionType??

Tony
________________________________________________________________________________
 
Obviously the asp runs once creating the html so I can't run the sub after the user clicks the submit button.

I don't mind putting the sub into another asp file but I don't know much about scope of variables here. Will I have to pass the recordset, recreate the connection to the database, etc. or will it stay set so I only need the sub in a new .asp file as it is?
 
Sorry Tony - we posted at the same time.

I set the ActionType in an admin page that has buttons for the user to Add, Edit, or Delete coupons. I then call this EditCoupon.asp file (shown above). I just thought it seemed redundant to have to have a seperate form and then processing asp file for each action. I thought it would seem more compact to create a recordset once, open a connection once, process accordingly, and close and reset the recordset once. Maybe I don't need to worry about keeping it all together. I just thought it would be easier on my end to maintain.
 
You still can keep it all together on one page. What you have now is nearly there.

I would agree with that keeping all the various Add/Edit/Delete methods on one page is easier to look after. Don't give up just yet :)

Try the suggestions above and let us know if you need more help.

I'm off home shortly but I'll check back in the morning :)

Tony
________________________________________________________________________________
 
I'm confused a bit about the above directions.
If I remove the code from within the sub, how do I keep it from running until the submit button is pressed? Do you recommend a flag be set when I call this EditCoupon.asp file? i.e. EditCoupon?Action=Add&?Pass=First (or Second)

Will the recordset and database connection be lost when you leave the current asp file? (Even if the file is calling itself)

Thanks for helping a newbie,
Amy
 
you could use
Code:
If Request.Form("B1") = "submit" Then

'your code here

End If

Tony
________________________________________________________________________________
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top