Ingus
Ingus

Reputation: 1044

Recapcha V3 doesnt work on page with multiple forms

i have an issue with Google Recaptcha V3. Id does work on single form but works only for first form if in page are more than 1 form. How to make it work for all forms? I know that issue is with id="recaptchaResponse" but i have no ideas how to fix this! There are smilar questions ou there but could not found a solution.

Javascript:

<script src="https://www.google.com/recaptcha/api.js?render=key1"></script>
<script>
    grecaptcha.ready(function () {
        grecaptcha.execute('key2', { action: 'contact' }).then(function (token) {
            var recaptchaResponse = document.getElementById('recaptchaResponse');
            recaptchaResponse.value = token;
        });
    });
</script>

Forms:

<form class="user" action="" method="post" name="form1" id="form1">
    <input type="hidden" name="source" value="form1">

    <button type="submit" class="btn btn-success">Submit 1</button>

    <input type="hidden" name="recaptcha_response" id="recaptchaResponse">
</form>

<form class="user" action="" method="post" name="form2" id="form2">
    <input type="hidden" name="source" value="form2">

    <button type="submit" class="btn btn-success">Submit 2</button>

    <input type="hidden" name="recaptcha_response" id="recaptchaResponse">
</form>

Please help! Thanks in advance!

Upvotes: 5

Views: 8282

Answers (2)

Carlos Puche
Carlos Puche

Reputation: 11

Remove the id tag and use only name tag.

grecaptcha.ready(function () {
    grecaptcha.execute({publicKey}, {action: 'forms'}).then(function (token) {
        var recaptchaElements = document.getElementsByName('recaptcha');
        for (var i = 0; i < recaptchaElements.length; i++) {
            recaptchaElements[i].value = token;
        }
    });
});

Upvotes: 1

jawish
jawish

Reputation: 606

The problem seems to be because the getElementById call only resolves the recaptcha_response input element in the first form and not the second.

A simple fix would be to change the ids of the recaptcha_response elements in each form to something different, say recaptchaResponse1 and recaptchaResponse2. Then the Javascript code to set the tokens could be:

grecaptcha.ready(function () {
  grecaptcha.execute('key2', { action: 'contact' }).then(function (token) {
    document.getElementById('recaptchaResponse1').value = token;
    document.getElementById('recaptchaResponse2').value = token;
  });
});

A better approach that is easier to maintain and will work for any number of forms is to specify a class name for the recaptcha_reponse inputs and use the querySelectorAll function to get all the inputs with the given class name and update them.

<form class="user" action="" method="post" name="form1" id="form1">
    <input type="hidden" name="source" value="form1">
    <button type="submit" class="btn btn-success">Submit 1</button>

    <input type="hidden" name="recaptcha_response" class="recaptchaResponse">
</form>

<form class="user" action="" method="post" name="form2" id="form2">
    <input type="hidden" name="source" value="form2">
    <button type="submit" class="btn btn-success">Submit 2</button>

    <input type="hidden" name="recaptcha_response" class="recaptchaResponse">
</form>
grecaptcha
  .execute("key", {
    action: "contact"
  })
  .then(function(token) {
    document
      .querySelectorAll(".recaptchaResponse")
      .forEach(elem => (elem.value = token))
    ;
  });

Hope that helps :)

Upvotes: 12

Related Questions