This question has been asked before: New Google ReCaptcha not posting/receiving 'g-recaptcha-response' - but there was no proper answer.
I have the exact same set up as him, but the code fails here:
if(!$captcha){
exit;
}
so $captcha=$_POST['g-recaptcha-response']
seems to be empty.
new google recaptcha with checkbox server side php = The 2nd answer here also doesn't seem to work.
Does anyone know why this could happen?
Check if you have the following present in the part where you show the form to the user:
Between
<form>
and</form>
:<div class="g-recaptcha" data-sitekey="your_public_key"></div>
Before the closing
</head>
tag:<script src='https://www.google.com/recaptcha/api.js'></script>
Check that your form uses
post
as method, ...<form method="post" ...>
If these are correct, at least some
$_POST['grecaptcha-response']
should be coming your way. Check those first in the resulting client side html code (in many browsers by pressingStrg
+U
while looking at the user-form) - rather than your server side code - it's easier to work with that knowledge. If all of those are in place even at the client, this will however be a tough one ^^I encountered this issue and found that my form was closing prematurely in the DOM because it was inside a table. ReCaptcha sets up a display:none g-recaptcha-response textarea and later fills in the data when you complete the captcha. It seems to look for children of the form that the div is in and thus couldn't find the g-recaptcha-response it had initially created. I put the form around the table and it worked fine after that.
If you are using reCaptcha v3, you have to explicitly define g-recaptcha-response in your form by attaching an ID to it.
If you are using reCaptcha v2, just make sure to put recaptchacontainer with in your form
None of the above worked for me. I'm using
react-google-recaptcha
. And it seems like you have to await/resolve therecaptchaRef.current.execute()
Promise beforehand.Without resolving the promise, it worked half the times, and that is when I call
recaptchaRef.current.getValue()
. Otherwise it would return an empty value.Just had the same problem. It was not a
<table>
tag causing the problem but it was a<div>
tag causing the problem.My form was within a main
<div>
used to format the general layout of the form. The<form>
tag doesn't HAVE to be within the main<div>
I was using for the form layout. I moved the<form>
tag just before my form layout<div>
tag and it started working perfectly.First check if recaptcha is set
Also have a look at this simple PHP tutorial for simple debugging.