Puja Verma
Puja Verma

Reputation: 11

Validate Captcha

I am using following code to access the captcha

<img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id="captchaimg" ><br />
<label for="message">Enter the code above here :</label>
<input id="6_letters_code" name="6_letters_code" type="text">

I am using jquery validation in the form, not able to validate the captcha with the right image, how can i validate it ? I have the below code for could be able to get it properly

if(empty($_SESSION['6_letters_code'] ) ||
  strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
    {
        //Note: the captcha code is compared case insensitively.
        //if you want case sensitive match, update the check above to
        // strcmp()
        $errors .= "\n The captcha code does not match!";
    }

Guide me Please ! Thankyou

Upvotes: 0

Views: 1555

Answers (1)

Manse
Manse

Reputation: 38147

You can add a rule like this :

rules: {
    6_letters_code: {
       required: true,
       remote: "process.php"
    }
}

where process.php is the PHP URL you want to use to check the CAPTCHA - all you need to do is return true or false.

Details of the remote option here

Your PHP should be something like :

if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_GET['6_letters_code']) != 0) {
   echo "false";
} else {
   echo "true";
}

It seems that the remote option uses get instead of post. There is a live example here you can use Firebug (or any browser debugger) to see the AJAX being sent/received

Upvotes: 1

Related Questions