-1

I have a form in my codeigniter project using google's invisible recaptcha like so:

HTML

<html>
<head>
  <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  <script>
  function onSubmitInvisRecaptcha(token) {
    document.getElementById("contact_us-form").submit();
  }
  </script>
</head>
<body>
<form id="contact_us-form" method="post" action="/info/contact_us">
  <div>
    <label>full name</label>
    <input type="text" name="full_name" value="<?php echo $this->input->post('full_name'); ?>"/>
  </div>
  <div>
    <button type="submit" 
    id="submit_btn" 
    class="btn my-other-styles g-recaptcha"
    data-sitekey="<?php echo $sitekey; ?>" 
    data-callback="onSubmitInvisRecaptcha">
      Submit
    </button>
  </div>
</form>
</body>
</html>

PHP

defined('BASEPATH') OR exit('No direct script access allowed');

class Info extends MY_Controller 
{
  function contact_us()
  {
    print_r($_POST);
  }
}

from my code I, I have 2 problems: (I hope it's ok to ask about multiple problems in 1 post)

  1. the recaptcha icon is nowhere to be found in the page. I've checked the sitekey I use in the form is the same as the one I find in www.google.com/recaptcha/admin.

  2. in the contact_us function, the print_r($_POST); there is no g-recaptcha-response..

P.S.: the form is a part of another page that is shown using ajax so the form is wrapped by another <div>.

dapidmini
  • 1,490
  • 2
  • 23
  • 46

1 Answers1

0

finally I've found the answer from this SO answer. The link shows a code for multiple recaptcha/form in one page, but it's simple enough to understand and modify for my needs.

basically, if I understand correctly, the reason my code failed was because of these points:

  1. I need to use a separate <div> element to apply the recaptcha instead of the submit button.

  2. google recaptcha will try to find the appointed element when the page loads, otherwise, I need to render and execute the grecaptcha manually using javascript if the element only appears or added to the page after some action.

dapidmini
  • 1,490
  • 2
  • 23
  • 46