如何检查数据从php页面返回到ajax成功函数

时间:2015-06-11 09:59:13

标签: php ajax if-statement

我想检查成功函数数据,使用下面ajax中的if循环是任何人帮助我的代码。

如果回声部分返回成功警报将会出现这个问题。

以下是javascript部分和php部分

    $(document).ready(function() {
            $("#newuserBtn").on("click", function() {
                $("#regform").show();
                $("#loginform").hide();
            });

            $("#submitBtn").on("click", function() {
                var uid = $("#uid").val();
                var pin = $("#inputPassword").val();
                $.ajax({
                    type: "POST",
                    url: "validation.php",
                    data: {
                        uname: uid,
                        pwd: pin
                    },
                    success: function(value) {
                        if (value == "success") { //problem here 
                            alert();
                        }
                    }
                });

            });

// php code


<?php
$servername = "localhost";
$username   = "root";
$password   = "ipad7*";
$db_name    = "users";

// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//Gets username value from the URL
$uname = $_POST['uname'];
$pwd   = $_POST['pwd'];

//Checks if the username is available or not
$query  = "SELECT pin FROM users_details WHERE uid = '$uname'";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while ($row = mysqli_fetch_assoc($result)) {
        if ($row["pin"] == $pwd) {
            echo "success";
        } else {
            echo "login fail";
        }
        //echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    //echo "login fail";
}
$conn->close();

//echo "fff";
//echo "$result";


?>

2 个答案:

答案 0 :(得分:0)

您是否在PHP脚本中关闭?>标记后检查是否没有额外的行结尾?如果有的话,当它与console.log显示时,字符串看起来仍然可以正常,但不相等 最好完全删除结束标记?>以避免这样的错误。

答案 1 :(得分:0)

试试这个:以responce编码格式发送您的json(游戏代码中的

<script>
   $(document).ready(function() {
            $("#newuserBtn").on("click", function() {
                $("#regform").show();
                $("#loginform").hide();
            });

            $("#submitBtn").on("click", function() {
                var uid = $("#uid").val();
                var pin = $("#inputPassword").val();
                $.ajax({
                    type: "POST",
                    url: "validation.php",
                    contentType: "application/json; charset=utf-8"
                    data: {
                        uname: uid,
                        pwd: pin
                    },
                    success: function(value) {
                        console.log(value);
                        if (value == "success") { //problem here 
                            alert();
                        }
                    }
                });

            });


</script>

<?php
$servername = "localhost";
$username   = "root";
$password   = "ipad7*";
$db_name    = "users";

// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//Gets username value from the URL
$uname = $_POST['uname'];
$pwd   = $_POST['pwd'];

//Checks if the username is available or not
$query  = "SELECT pin FROM users_details WHERE uid = '$uname'";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while ($row = mysqli_fetch_assoc($result)) {
        if ($row["pin"] == $pwd) {
            // echo "success";
            echo json_encode("success");
            exit();
        } else {
            // echo "login fail";
            echo json_encode("login fail");
            exit();
        }
        //echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    //echo "login fail";
}
$conn->close();

//echo "fff";
//echo "$result";


?>