PDA

View Full Version : asp script land user on a thank you page and email form data


tinah
09-30-2008, 06:37 PM
I have been working on this for over 3 days. I have a contact us form that is correctly gathering data and then "posting" to gdform.asp. But gdform.asp is not 1) landing the user on my thank you page 2) sending an email with the data that was submitted. HOW can I get it to do that? Here's the script:

<%

Dim landing_page, host_url
Dim fso, outfile, filename, dirname, myFolder
Dim req_method, key, value
Dim bErr, errStr, bEmpty
On Error resume next
bErr = false
bEmpty = true
errStr = ""
Set fso = Server.CreateObject("Scripting.FileSystemObject")
host_url = Request.ServerVariables("HTTP_HOST")
req_method = Request.ServerVariables("REQUEST_METHOD")
dtNow = Now()
filename = Server.MapPath("\ssfm")
dirname = filename
filename = filename & "\gdform_" & DatePart("M", dtNow) & DatePart("D", dtNow) & DatePart("YYYY", dtNow) & DatePart("N", dtNow) & DatePart("S", dtNow)

Function FormatVariableLine(byval var_name, byVal var_value)
Dim tmpStr
tmpStr = tmpStr & "<GDFORM_VARIABLE NAME=" & var_name & " START>" & vbCRLF
tmpStr = tmpStr & var_value & vbCRLF
tmpStr = tmpStr & "<GDFORM_VARIABLE NAME=" & var_name & " END>"
FormatVariableLine = tmpStr
end function

Sub OutputLine(byVal line)
outfile.WriteLine(line)
end sub

if err.number = 0 then
Set outfile = fso.CreateTextFile(filename, true, false)
if err.number <> 0 then
bErr = true
errStr = "Error creating file! Directory may not be writable or may not exist.<br>Unable to process request."
else
if(req_method = "GET") then
for each Item in request.QueryString
if item <> "" then
bEmpty = false
key = item
value = Request.QueryString(item)
if(lcase(key) = "redirect") then
landing_page = "thankyou.htm"
else
line = FormatVariableLine(key, value)
Call OutputLine(line)
end if
end if
next
elseif (req_method = "POST") then
for each Item in request.form
if item <> "" then
bEmpty = false
key = item
value = Request.form(item)
if(lcase(key) = "redirect") then
landing_page = "thankyou.htm"
else
line = FormatVariableLine(key, value)
Call OutputLine(line)
end if
end if
next
end if
outfile.close
end if
if(bEmpty = true) AND errStr = "" then
bErr = true
errStr = errStr & "<br>No variables sent to form! Unable to process request."
end if
if(bErr = false) then
if (landing_page <> "") then
response.Redirect "thankyou.htm"
else
response.Redirect "http://" & host_url
end if
else
Response.Write errStr
end if
set fso = nothing
else
Response.Write " An Error Occurred creating mail message. Unable to process form request at this time."
end if
%>

Spudhead
10-01-2008, 11:33 AM
Firstly, please put code into [ CODE ] braces - the easier you make it for people to help you, the more likely it is that they will.

Here's a slightly more readable version of your code:

Dim landing_page, host_url
Dim fso, outfile, filename, dirname, myFolder
Dim req_method, key, value
Dim bErr, errStr, bEmpty

On Error resume next

bErr = false
bEmpty = true
errStr = ""
Set fso = Server.CreateObject("Scripting.FileSystemObject")
host_url = Request.ServerVariables("HTTP_HOST")
req_method = Request.ServerVariables("REQUEST_METHOD")
dtNow = Now()
filename = Server.MapPath("\ssfm")
dirname = filename
filename = filename & "\gdform_" & DatePart("M", dtNow) & DatePart("D", dtNow) & DatePart("YYYY", dtNow) & DatePart("N", dtNow) & DatePart("S", dtNow)

Function FormatVariableLine(byval var_name, byVal var_value)
Dim tmpStr
tmpStr = tmpStr & "<GDFORM_VARIABLE NAME=" & var_name & " START>" & vbCRLF
tmpStr = tmpStr & var_value & vbCRLF
tmpStr = tmpStr & "<GDFORM_VARIABLE NAME=" & var_name & " END>"
FormatVariableLine = tmpStr
end function

Sub OutputLine(byVal line)
outfile.WriteLine(line)
end sub

if err.number = 0 then

Set outfile = fso.CreateTextFile(filename, true, false)

if err.number <> 0 then
bErr = true
errStr = "Error creating file! Directory may not be writable or may not exist.<br>Unable to process request."
else
if(req_method = "GET") then
for each Item in request.QueryString
if item <> "" then
bEmpty = false
key = item
value = Request.QueryString(item)
if(lcase(key) = "redirect") then
landing_page = "thankyou.htm"
else
line = FormatVariableLine(key, value)
Call OutputLine(line)
end if
end if
next
elseif (req_method = "POST") then
for each Item in request.form
if item <> "" then
bEmpty = false
key = item
value = Request.form(item)
if(lcase(key) = "redirect") then
landing_page = "thankyou.htm"
else
line = FormatVariableLine(key, value)
Call OutputLine(line)
end if
end if
next
end if
outfile.close
end if

if(bEmpty = true) AND errStr = "" then
bErr = true
errStr = errStr & "<br>No variables sent to form! Unable to process request."
end if
if(bErr = false) then
if (landing_page <> "") then
response.Redirect "thankyou.htm"
else
response.Redirect "http://" & host_url
end if
else
Response.Write errStr
end if

set fso = nothing

else
Response.Write " An Error Occurred creating mail message. Unable to process form request at this time."
end if

As to your questions:

1. I don't know. What IS it doing? Is it redirecting anywhere? Is it writing any error messages?

2. There's nothing in there that would send an email. To send an email in ASP, see here: http://www.w3schools.com/asp/asp_send_email.asp

tinah
10-01-2008, 10:32 PM
Answer to 1 is: yes, it's redirecting to my home page of my site. (action-engraving.com)
Questions about 2: where would I put this code? at the end of this script?

THANK YOU for your help.

Spudhead
10-02-2008, 03:39 PM
Ok. So you need to do some debugging. Look at it logically:

The bit that does the redirect is here:


if (landing_page <> "") then
response.Redirect "thankyou.htm"
else
response.Redirect "http://" & host_url
end if


Clearly, it's the second statement that's getting called, and sending people to the site root. That tells you that the variable landing_page is an empty string.

So... where does landing_page get set? Up here (there are two instances of this bit, one for a POST, one for a GET):

if(lcase(key) = "redirect") then
landing_page = "thankyou.htm"

If landing_page isn't getting set by that statement, you can assume that there's nothing in either the Form (POST) or QueryString (GET) collection that meets its condition - ie: there's nothing in there called "redirect".

So.. is there? Have you got something in your form called "redirect"? Are you sure it's getting sent correctly?


Regarding the email, once you've got the rest of it working I'd put it just before either of those redirect statements.

cesarcesar
10-28-2008, 02:07 PM
commented on wrong post