I am trying to implement google recaptcha v2 using ajax php but its not returning response after i solve it. i have two recaptchas, with different keys, first one works just fine.
after solving recaptcha: enter image description here
<div class="g-recaptcha" data-sitekey="key"></div>
$.ajax({
type: "POST",
url: "contact.php",
data: {
name: name,
email: email,
subject: subject,
message: message,
captcha: grecaptcha.getResponse()
}
}).done(function (response) {
var parsedResponse = JSON.parse(response);
if (!parsedResponse.success) {
$('#response').text(parsedResponse.message);
$('#contact_form_2')[0].reset();
} else {
$('#response').text("Wiadomość została wysłana.");
}
}).fail(function (jqXHR, textStatus, errorThrown) {
$('#response').text('Wystąpił błąd podczas wysyłania wiadomości.');
});
$recaptcha_secret = 'key';
$rresponse=$_POST["captcha"];
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_data = array(
'secret' => $recaptcha_secret,
'response' => $rresponse
);
$recaptcha_options = array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($recaptcha_data)
)
);
$recaptcha_context = stream_context_create($recaptcha_options);
$recaptcha_result = file_get_contents($recaptcha_url, false, $recaptcha_context);
$recaptcha_json = json_decode($recaptcha_result);
if (!$recaptcha_json->success) {
$response = array('success' => false, 'message' => 'Potwierdzenie reCAPTCHA nie powiodło się.');
echo json_encode($response);
return;
}
i was changing the code a lot, to the moment where captcha was working but it was redirecting to contact.php and not returing result to response as supposed. im in dead end