简体   繁体   English

reCAPTCHA Invisible - 重新提交表格问题

[英]reCAPTCHA Invisible - reSubmit Form Issue

While using reCaptcha, I faced a problem. 在使用reCaptcha时,我遇到了一个问题。 In code, using AJAX to submit form. 在代码中,使用AJAX提交表单。 Before submitting I need to check if the fields are filled or not. 在提交之前,我需要检查字段是否已填写。 If fields are not filled, submit should not happen. 如果字段未填写,则不应提交。

In this case, if textfields are not filled, it will give an alert. 在这种情况下,如果未填写文本字段,则会发出警报。

The problem is after the rejection of invalid post, the submit button stops working for like 2 or 3 minutes and there is no error given by reCaptcha. 问题是在拒绝无效帖子后,提交按钮停止工作2到3分钟,并且reCaptcha没有给出错误。 After the period of time, reCaptcha starts working again and submit button works. 经过一段时间后,reCaptcha再次开始工作并提交按钮。

<form id="contact-form" method="post" action="javascript:void(0)">
       <input type="text" placeholder="Name" id="name">
       <input type="text" placeholder="E-Mail" id="email">
       <textarea placeholder="Message" id="message">/textarea>
       <button class="g-recaptcha pull-right" data-sitekey="#your-site-key" data-callback="sendData" type="submit"> SEND <i class="flaticon-origami34"></i> </button>
</form>

Javascripts : Javascripts:

function sendData(){

    console.log('send data - '); // --> works

    //send datas: 

    $("#contact-form").submit();

};

$('#contact-form').on("submit", function() {

console.log('clicked submit'); // --> works

name = $('#name').val().replace(/<|>/g, ""), // prevent xss
    email = $('#email').val().replace(/<|>/g, ""),
    msg = $('#message').val().replace(/<|>/g, "");

if (name == '' || email == '' || msg == '') {

    alert("Please fill all areas !");

} else {

    console.log('captcha response: ' + grecaptcha.getResponse()); // --> captcha response: 

    // ajax to the php file to send the mail
    $.ajax({
        type: "POST",
        url: "SaveContact.php",
        data: "email=" + email + "&name=" + name + "&msg=" + msg + "&g-recaptcha-response=" + grecaptcha.getResponse()
    }).done(function(status) {
        if (status == "ok") {
            // clear the form fields
            $('#name').val('');
            $('#email').val('');
            $('#message').val('');
        }
    });

}});

According to the documentation , you can also call grecaptcha.reset(opt_widget_id); 根据文档 ,您还可以调用grecaptcha.reset(opt_widget_id); , where opt_widget_id is optional and will default to the first recaptcha widget created. ,其中opt_widget_id是可选的,默认为创建的第一个recaptcha小部件。

Then you need to reset cooldown of invisible recaptcha, you could use a hack like this 然后你需要重置隐形recaptcha的冷却时间,你可以使用这样的黑客

var recaptchaIframe = document.querySelector('.grecaptcha-badge iframe');
recaptchaIframe.src = recaptchaIframe.src;

I don't have a better solution. 我没有更好的解决方案。

if you render recaptcha explicitly in a script like this: 如果你在这样的脚本中显式渲染recaptcha:

var form = grecaptcha.render('idSelector', {
  'sitekey' : 'your_sitekey',
  'callback': callableFunction

}, true)

then, "form" variable contains widget_id, so you can reset that recaptcha like this 那么,“form”变量包含widget_id,所以你可以像这样重置那个recaptcha

grecaptcha.reset(form); 

Figured this out and wanted to post it in case anyone else has the same issue. 想出这个并想发布它以防其他人有同样的问题。 Google's example code set's the button id as "submit" and calling the "submit" function from within onSubmit was failing because of it. 谷歌的示例代码集的按钮ID为“提交”,并且在onSubmit内调用“提交”功能因此而失败。 Changing the id to anything else fixed it. 将id更改为其他任何修复它的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM