Google ReCaptcha not posting 'g-recaptcha-response'

36,731

Solution 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.

Solution 2

Check if you have the following present in the part where you show the form to the user:

  1. Between <form> and </form>:

    <div class="g-recaptcha" data-sitekey="your_public_key"></div>

  2. Before the closing </head> tag:

    <script src='https://www.google.com/recaptcha/api.js'></script>

  3. 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 ^^

Solution 3

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.

Solution 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.

Solution 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.

Share:
36,731

Related videos on Youtube

frosty
Author by

frosty

Updated on January 07, 2021

Comments

  • frosty
    frosty over 3 years

    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?

    • Marcel Burkhard
      Marcel Burkhard about 9 years
      Make sure you have the api js loaded in the <head> of your document and that there are no javascript errors. Additionally you could check the dom with Firebug or Chrome Dev Tools to see if the form field is there.
    • Michael K
      Michael K over 7 years
      Same issue here, my response is empty. Answers on both posts didn't help. Could there be a networking restriction that is blocking the response? The reCAPTCHA site doesn't show any data for mine yet, but maybe I have to wait more than a day.
    • Ryan
      Ryan over 3 years
      don't know if it helps you. but i downloaded a bootstrap template and i encountered this. the form is not actually posting, there's a js script that first validates data entry, empty fields etc. if all ok it builds up the POST request and posts it. i had to edit the js script to include the g-recaptcha-response field... hope this helps :)
  • frosty
    frosty about 9 years
    I found the "honeypot" method for keeping out spam bots and decided to go with that as it's far easier. Is there any reason why it I should use recaptcha instead though?
  • Levite
    Levite about 9 years
    @frosty: With a honeypot you are ONLY able to fool very general bots, if someone specifically targets you it is rather worthless. Also you have to be careful with people using form fillers (customers that let the browser fill their forms for them), which will be fooled as well, and the (well meaning) user might be supprised to be called a spammer by your site ^^. If you are fine with that, it is a valid option that will (only) protect you from the more general spambots. But I personally would recommend recaptcha for sure, it's not that complicated.
  • Levite
    Levite about 9 years
    @frosty: If you have checked the points of this answer, you only need to check the recaptcha when validating the form. See following answer for a ready-to-use function, and you are done: stackoverflow.com/questions/27274157/…
  • Jimmy Adaro
    Jimmy Adaro over 7 years
    Link no longer available.
  • robere2
    robere2 over 7 years
  • someone
    someone about 7 years
    I had the same issue in an old code that was never mine. I changed the position of <form> and </form> tags to outside the of the tags <table> and </table> and it is working now. Thanks zed.
  • Simos Fasouliotis
    Simos Fasouliotis over 6 years
    That was it for me too.
  • zanlok
    zanlok over 6 years
    yep! - implementing this on bad legacy code w/ <table><form>[table-contents]</form></table> and just sitched to <form><table>[table-contents]</table></form> this answer spared me 1 hour unnecessary debugging ! :)
  • Zohab Ali
    Zohab Ali almost 6 years
    In my case I did not add this in my head tag <script src='https://www.google.com/recaptcha/api.js'></script>
  • TarangP
    TarangP almost 5 years
    when you give google div id like this #g-recaptcha-response than it also didnt produce response. so dont give name like this.