简体   繁体   English

使用Ajax将reCaptcha令牌发送到PHP

[英]Sending reCaptcha token to PHP using Ajax

I want to add reCaptcha V3 to a form. 我想将reCaptcha V3添加到表单中。

Here is the form: 形式如下:

<form action="mail.php" method="POST">
    <!-- This hidden input will contain the token -->
    <input type="hidden" name="token" id="token" />
    <input type="text" name="name" />
    <input type="text" name="email" />
    <textarea name="message"></textarea>
    <input type="submit" name="submit" value="Send" />
</form>

Now I'm trying to send the token value to mail.php , So there is a plugin called jquery.form I use it to send the Ajax request. 现在,我尝试将token值发送到mail.php ,所以有一个名为jquery.form的插件,我用它来发送Ajax请求。

Here is the Javascript/Jquery code: 这是Javascript / Jquery代码:

$('form').ajaxForm({

    beforeSubmit: function() {

        //Captcha part
        grecaptcha.ready(function() {

                grecaptcha.execute('My_website_key', {action: 'form'}).then(function(token) {

                    //Set token value to the hidden element
                    $('#token').val(token);

                });

        });//reCaptcha ready    

    },//Before submit function
    success: function(msg) {

        if(msg == 'Message has been sent.'){
            console.log('success!');
        }else{
            console.log(msg);
        }

    },//success function
    complete: function(xhr) {

        console.log(xhr.responseText);  

    }//complete function

});//End Ajax

When I submit the form, Then I look at the console, I see that the token was empty and it seems that the success function is executed, Before the token hidden element gets the token. 当我提交表单时,然后查看控制台,我看到token为空,并且似乎执行了success函数,在token隐藏元素获得令牌之前。

I can't add the token when the page loads or any other action, Because it expires after 2 minutes, So I need to get aa token and send it to the PHP file. 当页面加载或任何其他操作时,我无法添加令牌,因为它会在2分钟后过期,所以我需要获取一个令牌并将其发送到PHP文件。

You need to move your ajax request in the recaptcha handler because beforeAjax will not wait for the its contents to be resolved, change your code as follows: 您需要在recaptcha处理程序中移动ajax请求,因为beforeAjax将不等待其内容被解析,请按如下所示更改代码:

 // Fake grecaptcha method !!! you should delete this // Form URL was changed as well to satisfy the example let grecaptcha = { ready: function(cb) { cb(); }, execute: function(key, options) { return Promise.resolve('recaptchatoken'); } }; let formElem = $('form'); formElem.submit(function(e) { e.preventDefault(); let thisForm = $(this); grecaptcha.ready(function() { grecaptcha.execute('My_website_key', { action: 'form' }).then(function(token) { $('#token').val(token); thisForm.ajaxSubmit({ success: function(msg) { if (msg == 'Message has been sent.') { console.log('success!'); } else { console.log(msg); } }, complete: function(xhr) { console.log(xhr.responseText); } }); }); }); return false; // false = prevent submit }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script src="http://malsup.github.io/min/jquery.form.min.js"></script> <form action="//reqres.in/api/users" method="POST"> <!-- This hidden input will contain the token --> <input type="hidden" name="token" id="token" /> <input type="text" name="name" /> <input type="text" name="email" /> <textarea name="message"></textarea> <input type="submit" name="submit" value="Send" /> </form> 

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

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