Click to See Complete Forum and Search --> : How to Prevent Form from Resetting


cdp103188
06-07-2007, 09:51 AM
OK, so disregard my last post, i have managed to write a validation script that causes a pop-up alert box to appear with all of the required fields that were not completed, however it reloads the page and resets the form at the same time, is there a way to prevent this? Here is the asp code that appears above my html.

<%
if Request.Form("isSubmitted") = "yes" then

Dim fname, lname, email, adrs1, adrs2, city, hstate, zip, hphone, wphone, cphone, state, comm, weap, game

Dim Mail

Dim Validate_Form, fnameCheck, lnameCheck, emailCheck, adrs1Check, cityCheck, stateCheck, zipCheck, ph1Check, ph2Check, ph3Check

fname = Trim(Replace(Request.Form("FName"),"""",""""""))
lname = Trim(Replace(Request.Form("LName"),"""",""""""))
email = Trim(Replace(Request.Form("Email"),"""",""""""))
adrs1 = Trim(Replace(Request.Form("Address1"),"""",""""""))
adrs2 = Request.Form("Address2")
city = Trim(Replace(Request.Form("City"),"""",""""""))
hstate = Trim(Replace(Request.Form("HState"),"""",""""""))
zip = Trim(Replace(Request.Form("Zip"),"""",""""""))
hphone = Trim(Replace(Request.Form("HomePhone"),"""",""""""))
wphone = Trim(Replace(Request.Form("WorkPhone"),"""",""""""))
cphone = Trim(Replace(Request.Form("CellPhone"),"""",""""""))
state = Trim(Replace(Request.Form("State"),"""",""""""))
comm = Request.Form("Comments")
weap = Request.Form("Weapon")
game = Request.Form("Game")

Validate_Form=true
dim alertMSG
alertMSG="Please Enter Your"

if len(fname)<1 then
Validate_Form = false
alertMSG = alertMSG + " First Name,"
end if

if len(lname)<1 then
Validate_Form = false
alertMSG = alertMSG + " Last Name,"
end if

if len(email)<1 or InStr(email,"@")=0 then
Validate_Form = false
alertMSG = alertMSG + " Email Address,"
end if

if len(adrs1)<1 then
Validate_Form = false
alertMSG = alertMSG + " Street Address,"
end if

if len(city)<1 then
Validate_Form = false
alertMSG = alertMSG + " City,"
end if

if len(hstate)<1 then
Validate_Form = false
alertMSG = alertMSG + " State,"
end if

if len(zip)<1 then
Validate_Form = false
alertMSG = alertMSG + " Zipcode,"
end if

if len(hphone)<1 and len(wphone)<1 and len(cphone)<1 then
Validate_Form = false
alertMSG = alertMSG + " Phone Number (any of the three)."
end if

if not Validate_Form then

Response.Write("<script language=JavaScript>alert('" & alertMSG & "');</script>")

else

Set Mail = Server.CreateObject("CDONTS.NewMail")

Mail.From = email

Mail.To = "cdp103188@gmail.com"

Mail.Cc = ""

Mail.Bcc = "cameron@huntingsportsplus.com"

Mail.Subject = "AWA Request Info Form Submission"

Mail.Body = "Name: " & fname & " " & lname & vbcrlf & vbcrlf & "Email: " & email & vbcrlf & vbcrlf & "Address: " & vbcrlf & adrs1 & ", " & adrs2 & vbcrlf & city & ", " & hstate & " " & zip & vbcrlf & vbcrlf & "Home Phone: " & hphone & vbcrlf & "Work Phone: " & wphone & vbcrlf & "Cell Phone: " & cphone & vbcrlf & vbcrlf & "State: " & state & vbcrlf & vbcrlf & "Weapon Preferance: " & weap & vbcrlf & vbcrlf & "Game Preferance: " & game & vbcrlf & vbcrlf & "Comments: " & vbcrlf & comm

Mail.BodyFormat = 1

Mail.MailFormat = 1

Mail.Send


end if

end if

%>

buntine
06-08-2007, 12:14 AM
I think you may be missing the point of server-side programming.

The ASP code can only run from the Web Server and thus the page must be refreshed in order to execute your validation script. Perhaps you can look into some prior validation with JavaScript?

It is always a good idea to validate form results on both the client and server sides.

Remember that once the page has reloaded, all the form results have been posted along with the request. This means you can repopulate the form on the fly if necessary.

Cheers,
Andrew.