I'm trying to create an ajax validation of Recaptcha and php with jQuery.
With jQuery, I'm trying to get the javascript to stop executing if the data returned has any value (which would be the recaptcha error) and to output the error. On the callback, and when there is no response from sendform.php the file that contains the captcha and my mail script, the error shows up fine and the recaptcha reloads, but "return false" does nothing. The script keeps going afterwards. I also can't create any variables and use them later in the script in the callback function. . . so is there any other way of doing this? Thanks
$.post(sendform.php, {recaptcha_response_field:$('#recaptcha_response_field').val(), recaptcha_challenge_field:$('#recaptcha_challenge_field').val(), captchaVerify:'true'},
function(data) {
if (data) {
output.html(data).slideDown(750);
Recaptcha.reload();
return false;
}
});
At this point in the code, I want the javascript to stop working here if sendform.php spits out an error (after these lines of code it continues on to submit the rest of the form data via $.post to send the email). However, return false doesn't do anything when it's inside a $.post callback function, the script just ignores it and keeps on going.
Here is the code in sendform.php. I have Recaptcha returning either its error if it fails, or nothing if it succeeds like this:
if (!$resp->is_valid) {
// What happ开发者_Go百科ens when the CAPTCHA was entered incorrectly
die('The reCAPTCHA wasn’t entered correctly. Please try it againg.' .'(reCAPTCHA said: ' . $resp->error . ')');
} else if (isset($_POST["captchaVerify"])) {
die();
}
Alright, I basically rearranged a lot of the code and called some already defined functions instead of trying to just return false and stop the script. I just had to approach it differently.
精彩评论