Click to See Complete Forum and Search --> : Passing parameters


hambo12
05-11-2010, 05:08 AM
Ok so im a bit lost. I have the following ASP page that updates a Database with the data input into the form. This is created by using a Dreamweaver template:

<%
Dim MM_editAction
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
Dim MM_abortEdit
MM_abortEdit = false
%>
<%
If (CStr(Request("MM_insert")) = "form") Then
If (Not MM_abortEdit) Then
' execute the insert
Dim MM_editCmd

Set MM_editCmd = Server.CreateObject ("ADODB.Command")
MM_editCmd.ActiveConnection = MM_CRM_STRING
MM_editCmd.CommandText = "INSERT INTO dbo.Address ([State], Town, Street, HouseNumber, UnitNumber) VALUES (?, ?, ?, ?, ?)"
MM_editCmd.Prepared = true
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 201, 1, 255, Request.Form("State")) ' adLongVarChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 201, 1, 255, Request.Form("Town")) ' adLongVarChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param3", 201, 1, 255, Request.Form("Street")) ' adLongVarChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param4", 201, 1, 255, Request.Form("HouseNumber")) ' adLongVarChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param5", 201, 1, 255, Request.Form("UnitNumber")) ' adLongVarChar
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
' append the query string to the redirect URL
Dim MM_editRedirectUrl
MM_editRedirectUrl = "Address_Results.asp"


<form name="form" method="POST" action="<%=MM_editAction%>">
State: <input type="text" name="State" maxlength="20" /><br><br>
Town: <input type="text" name="Town" maxlength="20"><br><br>
Street: <input type="text" name="Street" maxlength="20" /><br><br>
House Number: <input type="text" name="HouseNumber" maxlength="20" />
Unit Number: <input type="text" name="UnitNumber" maxlength="20" />
<input type="submit" name="Submit" value="Apply">
<input type="hidden" name="MM_insert" value="form" />
</form>

So this redirects the page to a new ASP page. however I cant work out how to display the values that have been added to the database.

I have tried request.form("State") etc, but cant get it to display.

What is the easiest way to do this?

yamaharuss
05-11-2010, 09:14 AM
Change your code to this:

<%
Dim MM_editAction
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
Dim MM_abortEdit
MM_abortEdit = false
%>
<%
If (CStr(Request("MM_insert")) = "form") Then
If (Not MM_abortEdit) Then
' execute the insert
Dim MM_editCmd

' BEGIN ADDITIONAL CODE
for x = 1 to request.form.count()
if request.form.item(x)<>"" then ' checks to see if item is blank - won't send blank items
if request.Form.key(x)<>"Submit" then
response.write "<b>" & (request.form.key(x) & "</b> = ")
response.write(request.form.item(x) & "<br>")
End If
End If
next
response.end
'END ADDITIONAL CODE



Set MM_editCmd = Server.CreateObject ("ADODB.Command")
MM_editCmd.ActiveConnection = MM_CRM_STRING
MM_editCmd.CommandText = "INSERT INTO dbo.Address ([State], Town, Street, HouseNumber, UnitNumber) VALUES (?, ?, ?, ?, ?)"
MM_editCmd.Prepared = true
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 201, 1, 255, Request.Form("State")) ' adLongVarChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 201, 1, 255, Request.Form("Town")) ' adLongVarChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param3", 201, 1, 255, Request.Form("Street")) ' adLongVarChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param4", 201, 1, 255, Request.Form("HouseNumber")) ' adLongVarChar
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param5", 201, 1, 255, Request.Form("UnitNumber")) ' adLongVarChar
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
' append the query string to the redirect URL
Dim MM_editRedirectUrl
MM_editRedirectUrl = "Address_Results.asp"


<form name="form" method="POST" action="<%=MM_editAction%>">
State: <input type="text" name="State" maxlength="20" /><br><br>
Town: <input type="text" name="Town" maxlength="20"><br><br>
Street: <input type="text" name="Street" maxlength="20" /><br><br>
House Number: <input type="text" name="HouseNumber" maxlength="20" />
Unit Number: <input type="text" name="UnitNumber" maxlength="20" />
<input type="submit" name="Submit" value="Apply">
<input type="hidden" name="MM_insert" value="form" />
</form>

hambo12
05-11-2010, 10:41 PM
Thanks for the help, ill try this when i get back to my computer.

Just so I am getting this straight, will this code pass the values to another ASP page, or will it display on the current page?

If it enables the information to be passed to a new ASP page, how do i call it from the new page??

hambo12
05-12-2010, 04:45 AM
OK so I tried this and when I click Submit it displays what I have input, however it does not add the information to the database.

What I am looking for is the submit button to apply the information to the Database (which it does), and then direct to a new page where the results are displayed.

How do I do this? Im sure its not hard, Im just new to all this...

yamaharuss
05-12-2010, 06:47 AM
I'm assuming you didn't write this yourself. What's with "MM_abortEdit = false"??

Where is your connection string? MM_CRM_STRING

I am also assuming the connection string exists. If not, nothing will work.



<%
Dim MM_editAction
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME")) & "?" & Server.HTMLEncode(Request.QueryString)


If (CStr(Request("MM_insert")) = "form") Then
' execute the insert
Dim sql, MM_editCmd


strState = Request.Form("State")
strTown = Request.Form("Town")
strStreet = Request.Form("Street")
strNum = Request.Form("HouseNumber")
strUnit = Request.Form("UnitNumber")

' I did not do any validation or cleaning.. you will need to add that


sql = "INSERT INTO dbo.Address ([State], Town, Street, HouseNumber, UnitNumber) VALUES ('"& strState &"', '"& strTown &"', '"& strStreet &"', '"& strNum &"', '"& strUnit &"')"
MM_CRM_STRING.Execute(sql)
MM_CRM_STRING.Close
Response.redirect ("Address_Results.asp")
%>

<form name="form" method="POST" action="<%=MM_editAction%>">
State: <input type="text" name="State" maxlength="20" /><br><br>
Town: <input type="text" name="Town" maxlength="20"><br><br>
Street: <input type="text" name="Street" maxlength="20" /><br><br>
House Number: <input type="text" name="HouseNumber" maxlength="20" />
Unit Number: <input type="text" name="UnitNumber" maxlength="20" />
<input type="submit" name="Submit" value="Apply">
<input type="hidden" name="MM_insert" value="form" />
</form>

The above code WILL work as long as MM_CRM_STRING is a valid db connection string.

hambo12
05-12-2010, 06:27 PM
haha your right, I didnt write this myself, I simply used the server behaviours within Dreamweaver to setup an insert function from the form I created. It may be better if I started again and tried to write it myself...

I will try the code you suggested and let you know how it goes.

Thanks for the help, I appreciate it.