如何在Codeigniter中使用jquery ajax使用Google Re Captcha?

时间:2019-02-23 19:44:41

标签: jquery ajax codeigniter recaptcha

代码:

<script>
    $(document).ready(function(){
        $("#register").click(function(e) {
            e.preventDefault();
            name = $("#name_up").val();
            $.ajax({
                type: "POST",
                url: "<?php echo base_url(); ?>register",
                data: {"name":name, "g-recaptcha-response": grecaptcha.getResponse()},
                success: function(data) {
                    alert(data);
                }
            });
        });
    });
</script>

<input type="text" name="name_up" id="name_up" placeholder="Name">
<div class="g-recaptcha" data-sitekey="******************************"></div>
<input class="theme-btn submit-btn" type="submit" name="register" id="register" value="Sign Up">

控制器:

public function register()
{
    $recaptcha = $this->input->post('g-recaptcha-response');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,"secret=*********************************=".$recaptcha);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = curl_exec ($ch);
    $server_output = json_decode($server_output,true);
    curl_close ($ch);
    if($server_output['success'])
    {
        echo "captacha validated successfully";
    }
    else
    {
        echo "invalid captcha";
    }
}

我创建了一个Recaptcha,它可以正常工作,但是问题是当我单击注册按钮而不检查Recaptcha时,它显示无效消息,但是当我选中Recaptcha复选框时,它又显示了无效消息。我该如何解决这个问题?请帮助我。

谢谢

0 个答案:

没有答案