towreg
February 15th, 2011, 02:21 PM
I am using a ASP script to parse an HTML contact-us page and send an email. I also implemented recaptcha into the ASP script and it's working, but I run into an issue when I attempt to response.Redirect the user to the "contact_success.html" page.
If I comment out the response.redirect line and use response.write it writes a blank message "thanks for your comment" fine, but I really want the regular look of my website experienced by going to "contact_success.html"
So my issue is I can't redirect to a confirmation page.
Below is the error I receive after successfully answering recaptcha and failing to redirect:
Microsoft VBScript runtime error '800a01a8'
Object required: ''
/contact_us/contact_us.asp, line 125
Below is my code (the error is pointing to the line "response.redirect(contact_suc...)":
<%@ Language="VBScript" %>
<%
Option Explicit
'------defining script vars-----------------------------------------------
Dim myBody, subject, mail_to, mail_from, pubkey, privkey, challenge, response, server_response
Dim test_captcha, smtpserver, message, show_form, form_feedback, newCaptcha
'-------Settings----------------------------------------------------------
smtpserver = "mail2-relay.iowa.gov"
subject = "I have a question/comment for Iowa HSEMD"
mail_from = "hsemd-comment@iowa.gov"
mail_to = "michael.stevens@iowa.gov"
pubkey = "6LenhMESAAAAAH9ZAkOqc-419ZIF8B_8RTdasY5R"
privkey = "6LenhMESAAAAAC3OAiSlLcNqtPH1dCYna6oDUuLy"
'challenge = Request.Form("recaptcha_challenge_field")
'response = Request.Form("recaptcha_response_field")
'------------------Function to Test Captcha Field-------------------------
' returns "" if correct, otherwise it returns the error response
function recaptcha_confirm(rechallenge,reresponse)
Dim VarString
VarString = _
"privatekey=" & privkey & _
"&remoteip=" & Request.ServerVariables("REMOTE_ADDR") & _
"&challenge=" & rechallenge & _
"&response=" & reresponse
Dim objXmlHttp
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.open "POST", "http://www.google.com/recaptcha/api/verify", False
objXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXmlHttp.send VarString
Dim ResponseString
ResponseString = split(objXmlHttp.responseText, vblf)
Set objXmlHttp = Nothing
if ResponseString(0) = "true" then
'They answered correctly
recaptcha_confirm = ""
else
'They answered incorrectly
recaptcha_confirm = ResponseString(1)
end if
end function
server_response = ""
newCaptcha = True
if (Request.Form("recaptcha_challenge_field") <> "" or Request.Form("recaptcha_response_field") <> "") then
server_response = recaptcha_confirm(Request.Form("recaptcha_challenge_field"), Request.Form("recaptcha_response_field"))
newCaptcha = False
end if
'---------------Example Function to Send Email Using CDOSYS
function sendMail(mail_to, subject, myBody, mail_from)
'send email
'Assemble message
myBody = myBody & vbCRLF & "Title: " & Trim(Request.Form("Title"))
myBody = myBody & vbCRLF & "First Name: " & Trim(Request.Form("FirstName"))
myBody = myBody & vbCRLF & "Last Name: " & Trim(Request.Form("LastName"))
myBody = myBody & vbCRLF & "Address1: " & Trim(Request.Form("Address1"))
myBody = myBody & vbCRLF & "Address2: " & Trim(Request.Form("Address2"))
myBody = myBody & vbCRLF & "City: " & Trim(Request.Form("City"))
myBody = myBody & vbCRLF & "State: " & Trim(Request.Form("State"))
myBody = myBody & vbCRLF & "Zip: " & Trim(Request.Form("ZIP_code"))
myBody = myBody & vbCRLF & "Day Phone: " & Trim(Request.Form("Daytime_phone"))
myBody = myBody & vbCRLF & "Email: " & Trim(Request.Form("Email"))
myBody = myBody & vbCRLF & "Topic: " & Trim(Request.Form("Topic"))
myBody = myBody & vbCRLF & "Question/Comment: " & Trim(Request.Form("Question/Comment"))
Dim cdoConfig, cdoMessage
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network)
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpserver
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
Set cdoMessage = CreateObject("CDO.Message")
With cdoMessage
Set .Configuration = cdoConfig
.Subject = subject
.From = mail_from
.To = mail_to
.TextBody = myBody
.Send
End With
Set cdoMessage = Nothing
Set cdoConfig = Nothing
end function
'''''''''' FORM REQUEST, PROCESSED ON POSTBACK ''''''''''''''
'Required - Correct Captcha Value
'test_captcha = recaptcha_confirm(challenge, response)
'response.write(test_captcha)
if server_response <> "" or newCaptcha then
if newCaptcha = False then
Response.Write("go back and try again")
end if
else
call sendMail(mail_to, subject, myBody, mail_from)
'Response.Write("thanks for your comment")
Response.Redirect("contact_success.html")
end if
%>
The script has no problems with Response.Write but I run into issues redirecting. Some variable needs to probably be set to NOTHING.
I'm not sure which variable is not resetting to nothing, but the script used to write my recaptcha response (whatever the user typed into the recaptcha field) to the page.
I fixed that issue somehow.
If I comment out the response.redirect line and use response.write it writes a blank message "thanks for your comment" fine, but I really want the regular look of my website experienced by going to "contact_success.html"
So my issue is I can't redirect to a confirmation page.
Below is the error I receive after successfully answering recaptcha and failing to redirect:
Microsoft VBScript runtime error '800a01a8'
Object required: ''
/contact_us/contact_us.asp, line 125
Below is my code (the error is pointing to the line "response.redirect(contact_suc...)":
<%@ Language="VBScript" %>
<%
Option Explicit
'------defining script vars-----------------------------------------------
Dim myBody, subject, mail_to, mail_from, pubkey, privkey, challenge, response, server_response
Dim test_captcha, smtpserver, message, show_form, form_feedback, newCaptcha
'-------Settings----------------------------------------------------------
smtpserver = "mail2-relay.iowa.gov"
subject = "I have a question/comment for Iowa HSEMD"
mail_from = "hsemd-comment@iowa.gov"
mail_to = "michael.stevens@iowa.gov"
pubkey = "6LenhMESAAAAAH9ZAkOqc-419ZIF8B_8RTdasY5R"
privkey = "6LenhMESAAAAAC3OAiSlLcNqtPH1dCYna6oDUuLy"
'challenge = Request.Form("recaptcha_challenge_field")
'response = Request.Form("recaptcha_response_field")
'------------------Function to Test Captcha Field-------------------------
' returns "" if correct, otherwise it returns the error response
function recaptcha_confirm(rechallenge,reresponse)
Dim VarString
VarString = _
"privatekey=" & privkey & _
"&remoteip=" & Request.ServerVariables("REMOTE_ADDR") & _
"&challenge=" & rechallenge & _
"&response=" & reresponse
Dim objXmlHttp
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.open "POST", "http://www.google.com/recaptcha/api/verify", False
objXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXmlHttp.send VarString
Dim ResponseString
ResponseString = split(objXmlHttp.responseText, vblf)
Set objXmlHttp = Nothing
if ResponseString(0) = "true" then
'They answered correctly
recaptcha_confirm = ""
else
'They answered incorrectly
recaptcha_confirm = ResponseString(1)
end if
end function
server_response = ""
newCaptcha = True
if (Request.Form("recaptcha_challenge_field") <> "" or Request.Form("recaptcha_response_field") <> "") then
server_response = recaptcha_confirm(Request.Form("recaptcha_challenge_field"), Request.Form("recaptcha_response_field"))
newCaptcha = False
end if
'---------------Example Function to Send Email Using CDOSYS
function sendMail(mail_to, subject, myBody, mail_from)
'send email
'Assemble message
myBody = myBody & vbCRLF & "Title: " & Trim(Request.Form("Title"))
myBody = myBody & vbCRLF & "First Name: " & Trim(Request.Form("FirstName"))
myBody = myBody & vbCRLF & "Last Name: " & Trim(Request.Form("LastName"))
myBody = myBody & vbCRLF & "Address1: " & Trim(Request.Form("Address1"))
myBody = myBody & vbCRLF & "Address2: " & Trim(Request.Form("Address2"))
myBody = myBody & vbCRLF & "City: " & Trim(Request.Form("City"))
myBody = myBody & vbCRLF & "State: " & Trim(Request.Form("State"))
myBody = myBody & vbCRLF & "Zip: " & Trim(Request.Form("ZIP_code"))
myBody = myBody & vbCRLF & "Day Phone: " & Trim(Request.Form("Daytime_phone"))
myBody = myBody & vbCRLF & "Email: " & Trim(Request.Form("Email"))
myBody = myBody & vbCRLF & "Topic: " & Trim(Request.Form("Topic"))
myBody = myBody & vbCRLF & "Question/Comment: " & Trim(Request.Form("Question/Comment"))
Dim cdoConfig, cdoMessage
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network)
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpserver
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
Set cdoMessage = CreateObject("CDO.Message")
With cdoMessage
Set .Configuration = cdoConfig
.Subject = subject
.From = mail_from
.To = mail_to
.TextBody = myBody
.Send
End With
Set cdoMessage = Nothing
Set cdoConfig = Nothing
end function
'''''''''' FORM REQUEST, PROCESSED ON POSTBACK ''''''''''''''
'Required - Correct Captcha Value
'test_captcha = recaptcha_confirm(challenge, response)
'response.write(test_captcha)
if server_response <> "" or newCaptcha then
if newCaptcha = False then
Response.Write("go back and try again")
end if
else
call sendMail(mail_to, subject, myBody, mail_from)
'Response.Write("thanks for your comment")
Response.Redirect("contact_success.html")
end if
%>
The script has no problems with Response.Write but I run into issues redirecting. Some variable needs to probably be set to NOTHING.
I'm not sure which variable is not resetting to nothing, but the script used to write my recaptcha response (whatever the user typed into the recaptcha field) to the page.
I fixed that issue somehow.