I've looked everywhere and have tried 10 different similar issues but neither works exactly how I need it to.
I have a simple form with validation using validationEngine (and this works fine, it validates the required fields and the captcha field and won't allow the jQuery .submit() function to process until they are valid). This code is supposed to collect the recaptcha fields, send them via AJAX post to the php handler, and receive a response, all the while using .submit() for non-invasive code:
Javascript:
function validateCaptcha() {
challengeField = $("input#recaptcha_challenge_field").val();
responseField = $("input#recaptcha_response_field").val();
var html = $.ajax({
type: "POST",
url: form.attr('action'),
data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
async: false
}).responseText;
if(html != "Valid") {
$("#captchaError").html('<p class="error">The security code you entered did not match. Please try again.</p>');
$captchaFlag = "Invalid";
Recaptcha.reload();
} else {
$("#memberInformation span").css({'color':'green'});
$("#memberInformation span").html(html.message).show(3000);
$("#captchaError").html('<p>Success!</p>');
$captchaFlag = "Valid";
dataString = form.serializeArray();
getSearchMembers(dataString);
}
}
formID.submit(function() {
var form = $(this);
if (formID.validationEngine('validate')) { //if it validates
$('#memberInformation span').html('');
//Captcha check
} //end if (formID.validationEngine('validate'))
else { //if does not validate
$('#memberInformation span').css('color','#ff0000').html('Please fill out required fields').show(3000);
} //end else
return false;
}); //end formID.submit
PHP
$privatekey = "1234567890"; //<!----- private key here
$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";
die ("The reCAPTCHA wasn't entered correctly. Please go back and try it again. (reCAPTCHA said: " . $resp->error . ")");
} else {
echo "Valid";
}
?>
Everything works except the sending of the _post data to the php file, so it always responds as "invalid". Any ideas. I want to use .submit(), and AJAX, and the current validator. Otherwise anything else can change all it needs to.