1

I am trying to add a recaptcha to one of my forms and I have no clue why it is no working.

In my form

... form ...
@if($settings->recaptcha_enabled && !empty($settings->recaptcha_site_key) && !empty($settings->recaptcha_secret_key))
{!! Form::hidden('g-recaptcha-response', null, [ 'id' => 'recaptcha', 'data-sitekey' => $settings->recaptcha_site_key]) !!}
                    @endif
... form ...

On the bottom of my page (the output is in HTML and has proper values as site key etc, so the condition is not a problem)

@if($settings->recaptcha_enabled && !empty($settings->recaptcha_site_key) && !empty($settings->recaptcha_secret_key))
    <script src="https://www.google.com/recaptcha/api.js?render={!! $settings->recaptcha_site_key !!}"></script>

    <script>
        function onClick(e) {
            e.preventDefault();
            grecaptcha.ready(function() {
                grecaptcha.execute('{!! $settings->recaptcha_site_key !!}', {action: 'submit'}).then(function(token) {
                    console.log('test1');
                    if (token) {
                        console.log('test2')
                        document.getElementById('recaptcha').value = token;
                    }
                });
            });
        }
    </script>
@endif

I can see a recaptcha badge on the bottom right corner so I suppose It has been loaded properly.

But when I click the submit form (with preserve log and enabled logging on the server) I am receiving NULL in g-recaptcha-response object. I´ve been looking for that hidden input change (there should be some addition of some token right before submitting the form I suppose) but that input wont change.

I am not posting here my backend part of validation (which should be pretty easy) because I have not managed the first part- getting recaptcha token from FE at first time.

Johnczek
  • 532
  • 9
  • 30

0 Answers0