I have a form that validates and posts with Ajax, or more specifically, with $.post().
At one point, there is a reCaptcha. This form worked fine when I did not use $.post() but posted the old-fashioned way (with a page refresh), just for reference.
Converting the form requires that I changed all $_POST‘s to $_REQUEST‘s. However, this doesn’t remedy the reCaptcha problem, so I’ve left that intact. Here is the reCaptcha PHP code inside registerPost.php:
require_once('recaptchalib.php');
$privatekey = "dropbeatsnotbombs";
$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 {
//post form
}
Here’s the jQuery handling the form posting:
$.post('php/registerPost.php', $('#registerPost').serialize(), function(data){
if(data == "Error")
{
$('#caErr').show();
}
else
{
window.location.replace("//main page");
}
});
Note: the reCaptcha seems to return as false, since the form will not submit when everything is filled in correctly.
What I want to do is show an error if the reCaptcha was entered wrong, and post if it was entered right, all asynchronously.
Any help?
Hmm. I need to go read the API documentation for $.post(), but my first guess is that since the form is not really submitted, the $_POST array is not being populated, so when you use values like
$_POST["recaptcha_challenge_field"], it’s just an empty field. Again, I need to go verify that, but that is my hunch.EDIT:
Looks like I was wrong. I compared what you have to both the API and an older script I had, and what you are doing should be fine. The only difference I see is that you are passing this as data:
whereas my script was passing this:
It could be as simple as that…