2

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;
}

2 Answers 2

3

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

 if (window.grecaptcha) {
    grecaptcha.reset();
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, final this one resolved my ajax call issue. but could not understand real issue.
-1

Try to use

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.