Recaptcha with Ajax error

Posted by Brandon on Stack Overflow See other posts from Stack Overflow or by Brandon
Published on 2012-12-12T18:01:41Z Indexed on 2012/12/13 5:04 UTC
Read the original article Hit count: 185

Filed under:
|
|
|

I'm having trouble validating a recaptcha input. Heres my code:

// Validate Recaptcha Input
var challenge = $("#recaptcha_challenge_field").val();
var response = $("#recaptcha_response_field").val();

var dataString = 'recaptcha_challenge_field=' + challenge + '&recaptcha_response_field=' + response;


var html = $.ajax({
type: "POST",
    url: "PHP/recaptchaverify.php",
    data: dataString,
    async: true
}).responseText;

console.log(html);

if(html == 'accept') 
{
    alert("CORRECT");
}

else
{
    alert("The reCAPTCHA wasn't entered correctly. Go back and try it again.");
    $("#recaptcha_response_field").focus();
    Recaptcha.reload();
    return false;
}

Now I passed my variables to recpatchaverify.php

require_once('../scripts/recaptcha-php/recaptchalib.php');
$privatekey = "MYKEY";

$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) 
{
    // What happens when the CAPTCHA was entered incorrectly
    echo "error";
} 

else 
{
    // Your code here to handle a successful verification
    echo "accept";
}

Now my problem is the html variable is displaying "accept" whenever I enter the Recaptcha correctly, but it won't work in the IF statement?

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery