SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Classic ASP Captcha - Missing Check Concept

    Hi everyone,

    I'm new to ASP and am trying to insert a basic captcha into a form. I can get the captcha to work fine; however, the moment I insert an action into the <form>, I receive error. On submit, I want the action to go to "thankyou.asp" where the form is then processed. This action insertion is making my captcha skip check. I don't understand the code enough to figure out how to fix this. Any help would be greatly appreciated. Code is below:

    In the Head of my Page:

    Code:
    <head>
    
        <script type="text/javascript" language="javascript">
            function RefreshImage(valImageId) {
                var objImage = document.getElementById(valImageId)
                if (objImage == undefined) {
                    return;
                }
                var now = new Date();
                objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
            }
        </script>
    
    	<script type="text/javascript">
    		function validateForm(myForm) {
    		
    			var reason = "";	
    		
    			reason += validateEmpty(myForm.firstname);
    			reason += validateEmpty(myForm.lastname);
    			reason += validateEmail(myForm.email);
    			reason += validateCaptcha(myForm.txtCaptcha);
    			
    			if (reason != "") {
    				alert("Please complete the required information.\n" + reason);	
    				return false;	
    			}
    			// alert("No errors found!");	
    			return true;
    		}	
    
    
    	
    		// validate the captch entry
    		function validateCaptcha() {
    	        var str1 = removeSpaces(document.getElementById('imgCaptcha').value);
    	        var str2 = removeSpaces(document.getElementById('txtInput').value);
    	        if (str1 == str2){
    				validateEmpty();
    			} else {    
    				//document.getElementById("txtInput").style.border = "2px solid rgb(255,0,0)";
    	        	return false;
    			}  
    	    }//end validateCaptcha()
    
    		// remove spaces from the captcha
    	    function removeSpaces(string) {
    	        return string.split(' ').join('');
    	    }//end removeSpaces()
    	</script>
    
    </head>
    My Form Code (just the captcha element is shown here):

    Code:
        <!--START FORM-->
        <form name="inputForm" id="inputForm" method="post" action="thankyou.asp" onsubmit="return validateForm(inputForm);">
    
    
    	    <!--Start Captcha-->
    	    <div style="text-align: center; margin-top: 20px;">
    	        <%
    	        if Request.ServerVariables("REQUEST_METHOD") = "POST" and IsEmpty(Request.Form("btnRetry")) then
    	            Dim lblResult, lblColor
    	            if IsEmpty(Session("ASPCAPTCHA")) or Trim(Session("ASPCAPTCHA")) = "" then
    	                lblResult = "This test has expired."
    	                lblColor = "red"
    	            else
    	                Dim TestValue : TestValue = Trim(Request.Form("txtCaptcha"))
    	                '//Uppercase fix for turkish charset//
    	                TestValue = Replace(TestValue, "i", "I", 1, -1, 1)
    	                TestValue = Replace(TestValue, "İ", "I", 1, -1, 1)
    	                TestValue = Replace(TestValue, "ı", "I", 1, -1, 1)
    	                '////////////////////
    	                TestValue = UCase(TestValue)
    	                
    	                if StrComp(TestValue, Trim(Session("ASPCAPTCHA")), 1) = 0 then
    	                    lblResult = "CAPTCHA PASSED"
    	                    lblColor = "green"
    	                else
    	                    lblResult = "CAPTCHA FAILED"
    	                    lblColor = "red"
    	                end if
    	                '//IMPORTANT: You must remove session value for security after the CAPTCHA test//
    	                Session("ASPCAPTCHA") = vbNullString
    	                Session.Contents.Remove("ASPCAPTCHA")
    	                '////////////////////
    	            end if
    	        %>
    	        <p><span style="color: <%=lblColor%>; font-weight: bold;"><%=lblResult%></span></p>
    	        <input type="submit" name="btnRetry" id="btnRetry" value="Take another test" />
    	        <%else%>
    	        <img src="captcha.asp" id="imgCaptcha" />&nbsp;<a href="javascript:void(0);" onclick="RefreshImage('imgCaptcha');">New Captcha</a><br />
    	        Write the characters in the image above<br />
    	        <input type="text" name="txtCaptcha" id="txtCaptcha" value="" /><br />
    	        <%end if%>
    	    </div>
    
    
    		<!--Submit Button-->
    		<p><button name="submit" type="submit">Submit Your Form</button></p>
    
    
        </form>
        <!--END FORM-->

  2. #2
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,249
    Mentioned
    113 Post(s)
    Tagged
    1 Thread(s)
    If I understand you properly, all you would need to do is add a response.redirect("thankyou.asp") inside the if statement where lblResult is set to "CAPTCHA PASSED". I would add the session remove code in thankyou.asp as well.
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style

  3. #3
    SitePoint Member
    Join Date
    May 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveMaxwell View Post
    If I understand you properly, all you would need to do is add a response.redirect("thankyou.asp") inside the if statement where lblResult is set to "CAPTCHA PASSED". I would add the session remove code in thankyou.asp as well.
    Thank you for your help Dave. This is what I thought was the case too, and I did try this. At first this looks like it works like a charm. However, the problem comes into play when you realize the thankyou.asp did not carry over the form content. The thankyou.asp page takes the form data and sends it to various e-mails. The e-mails were being sent, but they were empty. I lost the data carryover.

    So I'm stuck again :/

    Any thoughts? I am not clear about your second sentence regarding the removing of code in thankyou.asp. Please expand on this thought. I am unclear here.

    I appreciate your time and help.

  4. #4
    SitePoint Member
    Join Date
    Aug 2011
    Location
    Denmark
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have access to the file thankyou.asp?

  5. #5
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,249
    Mentioned
    113 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by creativeedg10 View Post
    Thank you for your help Dave. This is what I thought was the case too, and I did try this. At first this looks like it works like a charm. However, the problem comes into play when you realize the thankyou.asp did not carry over the form content. The thankyou.asp page takes the form data and sends it to various e-mails. The e-mails were being sent, but they were empty. I lost the data carryover.

    So I'm stuck again :/

    Any thoughts? I am not clear about your second sentence regarding the removing of code in thankyou.asp. Please expand on this thought. I am unclear here.

    I appreciate your time and help.
    Ah - that's a key piece that was missing. If you need to reference the request objects (post or querystring) on the next page, then you would use the server.transfer method instead of the response.redirect.

    As for the code, I was talking about the session clearing code - you'll need to carry that over to the thankyou.asp page so it gets removed.
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style

  6. #6
    SitePoint Member
    Join Date
    May 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveMaxwell View Post
    Ah - that's a key piece that was missing. If you need to reference the request objects (post or querystring) on the next page, then you would use the server.transfer method instead of the response.redirect.

    As for the code, I was talking about the session clearing code - you'll need to carry that over to the thankyou.asp page so it gets removed.
    Thank you Dave! I am getting a bit excited because I'm getting closer!

    By replacing response.direct with server.transfer, I was able to get the captcha to work in addition to having the form process with all field entries received! Yay!---but now I have one detail that is confusing to me that I'm stuck on:

    On submit, the form processes; however, the user is sent to the thankyou.asp page within the original form page itself. It's as though there was an iframe placed on the form page, and the thankyou.asp page is stuck in here. It looks very strange visually. The "iframe" is placed where the captcha is. Why is this occurring?

    I did not do anything to the thankyou.asp page...is my error occurring because of this? I don't understand how to session clear the code. I'm new to ASP, and this process is not familiar to me. What exactly will this do in the process, and how can I learn more? Perhaps that is the missing piece?

    Thank you again for your help Dave! I truly value and appreciate your time in educating me great skill concepts.

  7. #7
    SitePoint Member
    Join Date
    May 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a way I can trigger the server.transfer(thankyou.asp) to open in a new window to avoid the iframe effect thing going on? I'm not sure if that would solve the issue as I'm not entirely clear about what is causing it.

    The following is my captcha code:

    Code:
    <!--START FORM-->
    	    <form name="inputForm" id="inputForm" method="post" onsubmit="return validateForm(inputForm);">
    
                       <!--start captcha-->
                       <div style="text-align: center; margin-top: 20px;">
    		        <%
    		        if Request.ServerVariables("REQUEST_METHOD") = "POST" and IsEmpty(Request.Form("btnRetry")) then
    		            Dim lblResult, lblColor
    		            if IsEmpty(Session("ASPCAPTCHA")) or Trim(Session("ASPCAPTCHA")) = "" then
    		                lblResult = "This test has expired."
    		                lblColor = "red"
    		            else
    		                Dim TestValue : TestValue = Trim(Request.Form("txtCaptcha"))
    		                '//Uppercase fix for turkish charset//
    		                TestValue = Replace(TestValue, "i", "I", 1, -1, 1)
    		                TestValue = Replace(TestValue, "İ", "I", 1, -1, 1)
    		                TestValue = Replace(TestValue, "ı", "I", 1, -1, 1)
    		                '////////////////////
    		                TestValue = UCase(TestValue)
    		                
    		                if StrComp(TestValue, Trim(Session("ASPCAPTCHA")), 1) = 0 then
    							server.transfer("thankyou.asp")
    		                    lblResult = "Captcha Passed."
    		                    lblColor = "green"
    		                else
    		                    lblResult = "Captcha Failed. Try Again."
    		                    lblColor = "red"
    		                end if
    		                '//IMPORTANT: You must remove session value for security after the CAPTCHA test//
    		                Session("ASPCAPTCHA") = vbNullString
    		                Session.Contents.Remove("ASPCAPTCHA")
    		                '////////////////////
    		            end if
    		        %>
    		        <p><span style="color: <%=lblColor%>; font-weight: bold;"><%=lblResult%></span></p>
    		        <input type="submit" name="btnRetry" id="btnRetry" value="Take another test" />
    		        <%else%>
    			        <img src="captcha.asp" id="imgCaptcha" /><br />
    					<p><a href="javascript:void(0);" onclick="RefreshImage('imgCaptcha');">Give Me a New Captcha</a></p>
    			        <p>Type the characters in the image above</p>
    			        <input type="text" name="txtCaptcha" id="txtCaptcha" /><br />
    		        <%end if%>
    		    </div><!--/end captcha-->
    
    	    </form><!--/end form-->

  8. #8
    SitePoint Member
    Join Date
    May 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by creativeedg10 View Post
    Is there a way I can trigger the server.transfer(thankyou.asp) to open in a new window to avoid the iframe effect thing going on?
    I got the transfer to open in a new window, but this did not fix the problem. It is the same iframe problem, just a new window.

    Scratch that idea. lol


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •