将php变量传递给另一个文件中的JQuery ajax

时间:2016-08-24 10:54:50

标签: php jquery ajax

我一直试图将变量从php传递到Jquery,以确认INSERT查询是成功的。 首先,$msg变量默认为空字符串,然后mysqli_num_rows如果statement为true,$msg变量将不再为空 这是我的PHP       

$errfname = $errlname = $errusername= $errpassword = $msg= "";

$status=1;
$message="Success";

    if (empty($_POST['fname'])) {
        $errfname = "Please enter First Name";

        $emptyfname['emptyfname'] = $errfname;
        echo json_encode($emptyfname);

    }else{
        $fname = $_POST['fname'];
        $emptyfname['emptyfname'] = "";
        echo json_encode($emptyfname);

    }

    if (empty($_POST['lname'])) {
        $errlname = "Please enter Last Name";

    }else{
        $lname = $_POST['lname'];
    }

    if (empty($_POST['username'])) {
        $errusername = "Please enter First Name";
    }else{
        $username = $_POST['username'];
    }

    if (empty($_POST['password'])) {
        $errpassword = "Please enter Password";
    }else{
        $password = $_POST['password'];
    }

    if ($errfname=="" && $errlname=="" && $errusername=="" && $errpassword=="") {
        $exist = "SELECT * FROM users WHERE username='$username'";
        $result = mysqli_query($dbc, $exist);
        $row = mysqli_fetch_assoc($result);
        if (mysqli_num_rows($result)==1) {
              $status=0;
            $message="Usermame exist";
                        $array=array('status' => $status,
            'message' => $message);
        echo json_encode($array); 
        }

        else{
            $query = "INSERT INTO users (fname,lname,username,password) VALUES ('$fname','$lname','$username','$password')";
            $result = mysqli_query($dbc, $query);
            $_SESSION['username'] = $row['username'];
            $_SESSION['user_id'] = $row['id'];
        }

    }



?>

在js文件中我想传递php变量$msg,所以我可以用它做点什么。我想要做的是,如果变量$msg为空,这意味着它成功,那么我将用户重定向到主页

这是js

    $(document).ready(function(){
        $("#submit").click(function(e){
             var me = $(this);
             e.preventDefault();
             if ( me.data('requestRunning') ) {
                return;
            }

            me.data('requestRunning', true);
            var sendfname = $("#fname").val();
            var sendlname = $("#lname").val();
            var sendusername = $("#username").val();
            var sendpass = $("#pass").val();

                     if(sendfname == ""){
                        $("#fname").addClass("empty");
                        $("#fname").attr("placeholder", 'Please Enter First Name').focus().blur();
                    }
                     if(sendlname == ""){

                        $("#lname").addClass("empty");
                        $("#lname").attr("placeholder", 'Please Enter Last Name').focus().blur();
                    }
                    if(sendusername == ""){

                        $("#username").addClass("empty");
                        $("#username").attr("placeholder", 'Please Enter Username').focus().blur();
                    }
                    if(sendpass == ""){
                        $("#pass").addClass("empty");

                        $("#pass").attr("placeholder", 'Please Enter Password').focus().blur();
                    }

            $.ajax({
                type: 'POST',
                url: 'process.php',
                 dataType: 'json',
                data: "fname=" + sendfname + "&lname="+sendlname + "&username="+sendusername + "&password="+sendpass,
                success: function(data1){
                    if(data1.status==1){
                        alert(data1.message);//success
                    }else{
                        alert(data1.message);//error
                    }


                },
                 complete: function() {
                    me.data('requestRunning', false);
                }
            }); 

         return false;

        });

1 个答案:

答案 0 :(得分:0)

您的准则存在一些严重问题 首先在PHP文件中,您需要定义两个变量。一个用于状态,另一个用于消息

 $errfname = $errlname = $errusername= $errpassword = $msg= "";

 $status=1;
 $message="Success";

if (empty($_POST['fname'])) {
    $message = "Please enter First Name";
    $status=0;
}else{
    $fname = $_POST['fname'];
}

if (empty($_POST['lname'])) {
    $message = "Please enter Last Name";
    $status=0;
}else{
    $lname = $_POST['lname'];
}

if (empty($_POST['username'])) {
    $message = "Please enter First Name";
    $status=0;
}else{
    $username = $_POST['username'];
}

if (empty($_POST['password'])) {
    $message = "Please enter Password";
    $status=0;
}else{
    $password = $_POST['password'];
}

if ($errfname=="" && $errlname=="" && $errusername=="" && $errpassword=="") {
    $exist = "SELECT * FROM users WHERE username='$username'";
    $result = mysqli_query($dbc, $exist);
    $row = mysqli_fetch_assoc($result);
    if (mysqli_num_rows($result)==1) {          
        $message="Usermame exist";
        $status=0;                      
    }

    else{
        $query = "INSERT INTO users (fname,lname,username,password) VALUES ('$fname','$lname','$username','$password')";
        $result = mysqli_query($dbc, $query);
        if(!$result){
            $message="error";
            $status=0;
         }          
    }

}

  $array=array('status' => $status,
        'message' => $message);
    echo json_encode($array);  // send data as json format

你的jquery代码非常错误你在ajax响应成功后验证你的数据。在那之前验证它。在你的Jquery

  if(sendfname == "")
      {
         return;
      }
   //write all your validations then  write ajax
   $.ajax({
            type: 'POST',
            url: 'process.php',
             dataType: 'json',
            data: "fname=" + sendfname + "&lname="+sendlname + "&username="+sendusername + "&password="+sendpass,
            success: function(data1){
                 if(data1.status==1){
                    alert(data1.message);//success
                }else{
                    alert(data1.message);//error
                }
             }