O
O
Oleg Andrianov2021-12-13 16:37:36
PHP
Oleg Andrianov, 2021-12-13 16:37:36

Why is only the answer from the first captcha processed?

Good afternoon, dear Khabrovites!
I put one captcha on the site and everything worked well. Then I had to put the second and the problems started.
The bottom line is that the processing of the captcha response from the Google server occurs for the captcha that comes first in the DOM. In general, both forms with captcha separately work as they should, but together they don’t.
for the second captcha I can't get grecaptcha.getResponse();
In the headers of the php file, the g-recaptcha-response response is encoded.

Renaming grecaptcha objects to bind them to a specific dom node - didn't help.
Grecaptcha is an object, and it can be referenced somehow, I guess.
I thought that since there are two captchas on the page, there should be two grecaptcha objects, which means they must somehow be put into an array or into a collection and sorted out - it did not work out.
Addressed grecaptcha[0], grecaptcha[1] - hell there.

Is it possible to somehow refer to the second grecaptcha object, or does one overwrite the other?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Andrianov, 2021-12-13
@anov

He fiddled, fiddled, and the answer, as expected, was simple:

var recaptchaRequest;
var recaptchaRecall;
var onloadCallback = function() {
    // отрисовка recaptcha-request на элементе с id="recaptcha-request"
    recaptchaRequest = grecaptcha.render('recaptcha-request', {
      'sitekey' : 'публичный-ключ' 
    });
    // отрисовка recaptcha2 на элементе с id="recaptcha2"
    recaptchaRecall = grecaptcha.render('recaptcha-recall', {
      'sitekey' : 'публичный-ключ' 
    });
};

Zapara was that in the response it was necessary to substitute the response from the desired form during validation in formData.
For each form in its own validator.
var response = grecaptcha.getResponse(recaptchaRecall);
var response = grecaptcha.getResponse(recaptchaRequest);

A
Anton Shamanov, 2021-12-13
@SilenceOfWinter

grecaptcha.ready(function() {
          grecaptcha.execute('код', {action: 'url'}).then(function(token) {
              // Add your logic to submit to your backend server here.
          });
          grecaptcha.execute('код2', {action: 'url2'}).then(function(token) {
              // Add your logic to submit to your backend server here.
          });
        });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question