Before I start I am going to admit I know very little about asp coding and am looking to amend my code so it has form validation. I want the validation to require all fields are completed, a correct email address loaded and I want the code to go to a specific URL if it succeeds or fails rather than a line of text that is current displays. My current code is:
Set Mail = Server.CreateObject("SMTPsvg.Mailer") 'create an Asp mail component.
Mail.FromName = "Feedback"
Mail.FromAddress= Request.Form("email")
Mail.RemoteHost = "***remote_host_url***" ' The mail server you have to use with Asp Mail
Mail.AddRecipient "alliancepoliceforce.co.uk", "webmaster@alliancepoliceforce.co.uk"
Mail.Subject = "Website - Info Request"
Dim strBodyText
strBodyText = Request.Form("Feedback")
strBodyText=strBodyText & Request.Form("name") & vbcrlf
strBodyText=strBodyText & Request.Form("email") & vbcrlf
strBodyText=strBodyText & Request.Form("comments")
Mail.BodyText=strBodyText
if Mail.SendMail then
Response.Write "Your mail has already been sent..."
else
Response.Write "Mail send failure. Error was " & Mail.Response
end if
Set Mail = Nothing
%>
</body>
</html>
My question is how easy would it be to amend the code and where do I start? Any help would be greatly appreciated.
First, you should declare your form values ahead of your mail function, then validate them. You can do some further validation than exampled but you get the idea.
Code:
<% @LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<%
strName = TRIM(Request.Form("name"))
If strName = "" then
response.write("Please provide your name. Go Back.")
response.end
End If
strEmail = TRIM(Request.Form("email"))
If strEmail = "" then
response.write("Please provide your email address. Go Back.")
response.end
End If
strComments = TRIM(Request.Form("comments"))
Set Mail = Server.CreateObject("SMTPsvg.Mailer") 'create an Asp mail component.
Mail.FromName = "Feedback"
Mail.FromAddress= Request.Form("email")
Mail.RemoteHost = "***remote_host_url***" ' The mail server you have to use with Asp Mail
Mail.AddRecipient "alliancepoliceforce.co.uk", "webmaster@alliancepoliceforce.co.uk"
Mail.Subject = "Website - Info Request"
Dim strBodyText
strBodyText = Request.Form("Feedback")
strBodyText=strBodyText & strName & vbcrlf
strBodyText=strBodyText & strEmail & vbcrlf
strBodyText=strBodyText & strComments
Mail.BodyText=strBodyText
Mail.SendMail
response.redirect("ThankYou.asp")
response.end
Set Mail = Nothing
%>
</body>
</html>
Thank you for your response. I have added the code in as suggested and it comes back with the relevant form validation. However, is there anyway I can do it so that it just appears on the same page. So for example, if I had just completed the form and click send, rather than just go to a white screen with 'You need to fill in your email' is it possible to have it just appear on the same page, maybe just above the fields. Also, how do I amend the code to go to a specfic URL once the forms have been successfuly submitted.
The way I wanted it to go is that the validation would verify that all fields had been completed and that the email entered was valid. Once the user has then clicked on submit they are redirected to http://www.alliancepoliceforce.co.uk...n/success.html
If they have not filled in all the forms correctly how would I do it so that it redirects to failed.html but then states specfically which fields they have failed to complete accurately.
I guess I need to know how to change the code so it redirects to those pages and then how to add error message to the failed.html when it does.
By the way before continueing just want to say thank you for your help on this.
Bookmarks