First of all, I am aware that the same problem was already discovered here: Error: No reCAPTCHA clients exist (reCAPTCHA v3) But since the answers there, didn't lead me to a solution, I try my luck here.
So I tried using reCAPTCHA, since I get alot of spam mails from the form on my webpage. In my HTML head I have that code:
<script src="https://www.google.com/recaptcha/api.js?render=MY_SITE_KEY"></script>
<script>
grecaptcha.ready(function() {
grecaptcha.execute("MY_SITE_KEY").then(function(token) {
console.log(token);
});
});
</script>
to load the Captcha and which generates a token. When I submit my form, I call the following ajax code:
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: {
name: name,
email: email,
message: message,
captcha: grecaptcha.getResponse()
}).done(function(response){ ... })
and finally in PHP I do the following:
<?php
$secret = "MY_SECRET_KEY";
$response = $_POST["captcha"];
$verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo "reCaptcha indentified you as a bot. We don't like bots here.";
}
else if ($captcha_success->success==true) {
// MY WHOLE mail() function here
}
?>
When I submit the form then, I get the error:
Uncaught Error: No reCAPTCHA clients exist.
at Gw (recaptcha__de.js:511)
at Object.Q5 [as getResponse] (recaptcha__de.js:519)
at HTMLFormElement.<anonymous> (main.js:265)
at HTMLFormElement.dispatch (jquery.min.js:3)
at HTMLFormElement.q.handle (jquery.min.js:3)
What have I done wrong? I followed Googles instructions, but maybe I missed something.
grecaptcha.ready(function() { ...}
logging a token to the console? What'sgrecaptcha.getResponse()
returning in your browsers dev console?grecaptcha.getResponse()
is not returning anything tho :/console.log(token)
with:var recaptchaResponse = document.getElementById('recaptchaResponse'); recaptchaResponse.value = token;
and add following input tag to your form:<input type="hidden" name="recaptcha_response" id="recaptchaResponse">
? In your ajax, you would then just replace thegrecaptcha.getResponse()
withdocument.getElementById('recaptchaResponse').value
-- And see if it works?