如何使用ajax将recaptcha响应数据发送到php文件

时间:2017-09-14 08:10:15

标签: php html ajax

我想从ajax发送数据但是发错了。我也接受了ReCaptcha 2.0 With AJAX的帮助,但我仍然在php文件中遇到相同的POST错误。

$("#submit").click(function(){

    var name = $("#name").val();
    var mobile = $("#mobile").val();
    var email = $("#email").val();
    var subject = $("#subject").val();
    var message= $("#message").val();
    if(name=='' || mobile=='' || email=='' || subject=='' || message==''{
        $("#errmsg").html("All fields are required");
    } else {
        //$("#reqmsg").html(username);

        $.ajax({
            type: "POST",
            url: "insertquery.php",
            data: "name="+name+"&mobile="+mobile+"&email="+email+"&subject="+subject+"&message="+message+"&g-recaptcha-response="+grecaptcha.getResponse(),
             success: function(data){    
                if(data=="ok"){
                   alert("query submitted");
                   window.location="http://www.intuitioninteriors.in";
                } else {
                   $("#errmsg").html("some went wrong.please try again");
                }
            }
        });
        return false;       
    }
});

上面是ajax文件。

include("database.php");
$name=$_POST["name"];
$mobile=$_POST["mobile"];
$email=$_POST["email"];
$subject=$_POST["subject"];
$message=$_POST["message"];
$captcha = "";
if (isset($_POST["g-recaptcha-response"]))
     $captcha = $_POST["g-recaptcha-response"];
$url="http://www.google.com/recaptcha/api/siteverify";
$privatekey="xxxxxxxxxxxxxxxxxxxxxxxxx";

$response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".$captcha."&remoteip=".$_SERVER["REMOTE_ADDR"]), true);

if ($response["success"] != false) {
    $query="insert into query values(NULL,'$name','$mobile','$email','$subject','$message')";
    $result=mysqli_query($conn,$query)or die(mysqli_error($conn));
    if($result){
        echo 'ok';
    } else {
        echo 'not ok';
    }
} esle{
    echo 'not verified';
}

这是php文件。

错误:POST http://intuitioninteriors.in/insertquery.php 500(内部服务器错误)

我该如何解决?

0 个答案:

没有答案