【问题标题】:Multiple Google Recaptchas in a same page同一页面中有多个 Google Recaptchas
【发布时间】:2017-06-05 21:52:03
【问题描述】:

我在这里遇到问题,我在我的页面上包含了 2 个重新验证码,它们工作正常,但是在清理它们时(在通过 ajax 提交后)我无法重置重新验证码,如果我使用“grecaptcha .reset”它只擦除一个recaptcha .. 谢谢!

【问题讨论】:

  • 为什么首先要有两个?
  • 因为我在同一个页面中有两个表单:与我们合作和联系。
  • 尝试将验证码 ID 添加到数组中,然后重置该数组中的每个 ID?

标签: javascript recaptcha


【解决方案1】:

这是我的简单解决方案,这样您就可以在同一页面上准备尽可能多的验证码。记得放google api元素:SiteKey和SecretKey!

后端:

<?php

$siteKey = 'Paste element provided by google api';
$secretKey = 'Paste element provided by google api';

if($_POST['submit']){

    $username = $_POST['username'];
    $responseKey = $_POST['g-recaptcha-response'];
    $userIP = $_SERVER['REMOTE_ADDR'];
    $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
    $response = file_get_contents($url);
    $response = json_decode($response);
    if($response->success){
        echo "Verification is correct. Your name is $username";
    } else {
        echo "Verification failed";
    }
}?>

还有前端:

<html>
<meta>
    <title>Google ReCaptcha</title>
</meta>
<body>
    <h3>First form</h3>
    <form action="index.php" method="post">
        <input type="text" name="username" placeholder="Write your name"/>
        <div id="recaptcha1"></div>
        <input type="submit" name="submit" value="send"/>
    </form>


    <h3>Second form</h3>
    <form action="index.php" method="post">
        <input type="text" name="username" placeholder="Write your name"/>
        <div id="recaptcha2"></div>
        <input type="submit" name="submit" value="send"/>
    </form>


    <script src="https://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>
    <script>
        var recaptcha1;
        var recaptcha2;
        var myCallBack = function() {

            recaptcha1 = grecaptcha.render('recaptcha1', {
                'sitekey' : '<?= $siteKey ?>',
                'theme' : 'light'
            });

            recaptcha2 = grecaptcha.render('recaptcha2', {
                'sitekey' : '<?= $siteKey ?>',
                'theme' : 'dark'
            });
        };
    </script>
</body>

干杯!

【讨论】:

    猜你喜欢
    • 2010-11-17
    • 2012-01-12
    • 2020-02-03
    • 2013-02-10
    • 2011-08-03
    • 2020-05-09
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多