ivva
ivva

Reputation: 2949

Using Google Recaptcha with ajax - not loading again

I have issue with Google recaptcha. I'm using Ajax to send form data and recaptcha session expires and user should validate again captcha. But if has been checked once, it is displayed as checked chechbox and user should reload the page...

I'm using PHP validation for recaptcha response to validate.

But what is the correct solution for recaptcha reloading - Is it ok only to add js validation like that or there is better solution:

var v = grecaptcha.getResponse();

if(v.length == 0)
{
  $('<label class="error captcha_field">This field is required!</label>').insertAfter($(".g-  recaptcha").closest('.form-group'));
  return false;
}

Now, I tried with the following code but it returned js error:

response is not defined

var captcha = grecaptcha.getResponse();
if (captcha == 0){

  $('<label class="error       captcha_field">'+response.errors.captcha_failed+'</label>').insertAfter($(".g-recaptcha").closest('.form-group'));
  return false;
}

Upvotes: 2

Views: 1428

Answers (2)

Ryan
Ryan

Reputation: 27

Try to use

if(window.grecaptcha) {
  grecaptcha.render('widget id')
}

Upvotes: -1

ivva
ivva

Reputation: 2949

I found solution. In ajax response I reload captcha in that way:

 if (window.grecaptcha) {
    grecaptcha.reset();
 }

Upvotes: 3

Related Questions