JavaScript for time delay onClick goto URL for Form submission

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rahina
    New Member
    • Mar 2008
    • 1

    JavaScript for time delay onClick goto URL for Form submission

    I am working in Dreamweaver 8, and I have created a simple form: http://www.realmofmyst ery.com/app.html

    This is a free website I am doing for some friends, so nothing fancy. And, I learned web design before all the CGI and PHP stuff got big, so I prefer to do things simply.

    The form worked fine, emails the results like perfect, but it refused to redirect the page to the confirmation. I thought I could add an onClick to the submit button, but this cancels out the form processing.

    So, I called my hosting people, they told me to redirect the form action from a mailto, to the gdform.asp file on the server. I did this, and I keep getting errors now. It refuses to work. They are no help, they say I made a mistake scripting, only I didn't script anything!!! I am just using their script. So, I think this could be solved with a simple piece of JavaScript, problem is, I don't know enough about JavaScript to write my own script, but I am assuming the onClick would work if there was also a time delay written into the script that gave the form enough time to submit before redirecting to the confirmation page.

    Does anyone have a script to delay the onClick event so I can bypass this?

    If you need any extra info, let me know. Also, here is the code contained in my gdform.asp:

    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 = value
    					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 = value
    					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 "http://" & host_url & "/" & landing_page
    		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
    %>
    When I first changed the form to direct here, I got the first error it mentions, then when I removed the JavaScript to validate the form on submission, I started getting the second error.

    Thanks in advance!
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    This seems like an ASP problem rather than a JavaScript one. I'll move this to the ASP forum.

    Have you made sure the form field names match those required by the script?

    Comment

    • pronerd
      Recognized Expert Contributor
      • Nov 2006
      • 392

      #3
      Originally posted by Rahina
      I am assuming the onClick would work if there was also a time delay written into the script that gave the form enough time to submit before redirecting to the confirmation page.
      That would not be a good solution since the user would see the confirmation page, even if an error occurred and their submition did not go though. You really want your server side logic to process the request, then if the request was successfully processed forward them to the confirmation page. Otherwise you want to return an error so they know the request did not go though.

      Also, as soon as you submit a request the current page stops executing, so it can not then submitted a second request later. So the only way for what you describe to work would be for the first request to be submitted via AJAX, and then submit the second request to the confirmation page.

      Comment

      Working...