可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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.
回答2:
Today I had this same issue (g-recaptcha-response
had no value upon submit) on a website of a colleague. Turns out that the tag <form
was mistakenly nested right after the opening tag <table
(not inside a td, but directly after <table
).
This was causing the issue.
After moving the tag form
in way it wrapped the table
, the value of g-recaptcha-response
was correctly posted to server side after submit.
回答3:
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 ^^
回答4:
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.
回答5:
It seems that Google wants your opening and closing tags to be outside of other DOM elements such as <table>
or <div>
. I had the exact same issue which is now resolved. Move your...
<div class="g-recaptcha" data-sitekey="abcd1234etc."></div>
...code to outside of any or tag and it will work. Seems as if Google cannot find the form and inject its form value otherwise.
回答6:
First check if recaptcha is set
if(!isset($_POST['g-recaptcha-response']) ){
die ("Error: Not valid recaptcha on form");
}
Also have a look at this simple PHP tutorial for simple debugging.
回答7:
Yes, the error is the DOM
Code error
table width="100%" border="0" cellspacing="0" cellpadding="0" align="center"
form id="contacto" name="contacto" method="post" action="xxx"
Code OK
form id="contacto" name="contacto" method="post" action="xxx"
table width="100%" border="0" cellspacing="0" cellpadding="0" align="center"
回答8:
The perfect solution is do not create your own g-recaptcha-response input because google will fill it with the response and still yet go ahead creating another g-recaptcha-response textarea but then will not fill it with the response value
回答9:
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
回答10:
None of the above worked for me. I'm using react-google-recaptcha
. And it seems like you have to await/resolve the recaptchaRef.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.