tags:

views:

669

answers:

4

Hi I'm a relative newbie. Have a mail contact form set up with a captcha image generator. When the captcha is verified, on submitting the form, a php page is actioned which further validates the input data (checking against spam). Challenge: would like to retain form data in case of error in enterred capthca code and needing to return to form. If I use a sticky form with the form sticks okay but I cannot see how I then direct http to the php script for form data validation. So I figure the answer is a javascript function to validate the captcha and stay within the same page where the form appears, ideally just having a pop up message (alert ...) if the enterred code is wrong, before sending the http to the php script page.
I have seen that this can be done but I cannot adapt the code to the captcha I use (i.e. webspamprotect.com) Could any body suggest a generic js function useable with any captcha ? Would be most grateful for any input. MANY THANKS Steve

A: 

When the captcha is generated the written word is usually stored inside the session or written into a hidden input field, so it can be validated against the user supplied word, when the form is submitted back to the PHP script.

You could pass the session variable holding this to your JavaScript (or read it from the hidden input if present) when rendering the page holding the form and captcha. Then, when the user clicks submit, intercept the call and check if the entered word matches the expected word.

As for retaining the values: just add the values to your HTML form value attribute. Make sure you escape the output in case users supply malicious code.

EDIT: agreeing with everyone who says you still have to validate the input on the server side as well. Client Side validation can easily be tinkered with and is nothing but a convenience feature for users, so they can fix their input before submitting.

Gordon
A: 

It doesn't make sense to be able to validate the CAPTCHA with javascript on the client. If you made it possible to validate with Javascript a scammer could use the validation function to test their guesses before they sent them to the server, so they would always be able to get the answer right.

You could implement an AJAX call requesting the server to validate the attempt instead of requiring a full page refresh, but the validation must still be done on the server.

Mark Byers
A: 

Hi Guys Thanks for the response. I would very much like to use spry validation which i can easily style with css and make a neat self validating form. I would happily then just send http to an error message page if the captcha is wrong and use a js history -1 call back to the form, however the data does NOT stick when using spry validation, even adding value attributes to the form. (Incidentally I don't understand how sticky forms are of use if they work by POST to SELF?...i.e. how is http directed to the form processing php script?) So I guess you are suggesting it can work using a JS function to intercept the captcha session variable. I hear what is said about the risk of validation on client side, but the js is only to validate the captcha, then http would go to a php script with spam filter coding...is that good enough protection? Being not just a newbie , but a very green newbie, is it a lot to ask for a sample javascript to intercept the captcha ? I'm guessing something like Not sure of how exactly to access the element of captcha that I'm checking the entered code against. MANY THANKS

Steve
-1 Answers aren't for questions - they're for answers. Either update your question or create a new one.
Mark Byers
A: 

Hi again I wrote some sample script in the previous message but its not shown up. I guess theres a right and wrong way top enter coding. Cant see where it tells me how. Can somenone let me know please! trying again here

<script... function checkcaptcha (){ var $code = 'code'; if ($code !=OK) window.location="http://processform.php"; else alert ("captcha error, try again") script>

Steve