PHP和Ajax并不总是返回响应

时间:2017-03-19 15:48:38

标签: javascript php jquery html ajax

我是网络编程的新手。目前我一直在努力建立一个登录/注册网站。这是我的HTML代码:

<!DOCTYPE html>
<html>
<head>
<link href="test.css" type = "text/CSS" rel = "stylesheet" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" src="scrpt.js"></script>
</head>
<body>
<form>
    <label> First Name: </label>
    <br>
    <input class = "inp" type = "text" name = "fname" id = "fname"> </input>
    <br>
    <label> Last Name: </label>
    <br>
    <input class = "inp"  type = "text" name = "lname" id = "lname"> </input>
    <br>
    <label> Email: </label>
    <br>
    <input class = "inp"  type = "text" name = "email" id = "email">     </input>
    <br>
    <label> Username: </label>
    <br>
    <input  class = "inp" type = "text" name = "usrname" id = "usr">     </input>
    <br>
    <label> Password: </label>
    <br>
    <input  class = "inp" type = "password" name = "psw" id = "psw"> </input>
    <br>
    <input type = "submit" id = "submit" >  </input>
    <br>
    <label> Login: </label>
    <br>
    <input class = "inp" type = "text" name = "login" id = "log"> </input>
    <br>
    <label> Password: </label>
    <br>
    <input  class = "inp" type = "password" name = "logPsw" id = "logPsw"> </input>
    <br>
    <input type = "submit" id = "logSub" >  </input>
</form>

</body>
</html>

这是我的JavaScript代码:

$(document).ready(function(){
$("#submit").click(function(){
    var fn = $("#fname").val();
    var ln = $("#lname").val();
    var email = $("#email").val();
    var usr = $("#usr").val();
    var psw = $("#psw").val();
    $.ajax(
    {
    type: "POST",
    url: "register.php", 
    data: {
        fname: fn,
        lname: ln,
        email: email,
        usr: usr,
        psw: psw
    },
    success: function(result){

    }
    }
    );
});

$('#logSub').click(function(){
    var login = $("#log").val();
    var psw = $("#logPsw").val();
    $.ajax(
    {
        type: "POST",
        url: "login.php",
        data: {
            login: login,
            password: psw
        },
        success: function(result){
            alert(""+result);
        }

    }
    );
});
});

这是我的PHP代码:

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "admin_db";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}



$login = $_POST['login'];
$psw = $_POST['password'];


$sql = "select * from user where userName = \"$login\" and password = \"$psw\"";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "Yes";
}
else{
    echo "No Such Account";
}
$conn->close();

?>

这里的问题是,如果我输入了错误的登录名/密码,它应该提醒“没有这样的帐户”,但是5次尝试中有1次没有。首先它做,第二次它做,第三次尝试后它只是没有说什么,然后在第四次尝试后它继续正常工作。你能告诉我原因吗?

P.S我试图在这里和其他论坛上浏览每一篇文章,但没有一篇看起来有用,所以如果这篇文章重复,请告诉我,我会删除它。

1 个答案:

答案 0 :(得分:0)

问题的主要原因是,由于没有特定的操作和方法设置形成,它会自动使用'GET'方法提交,因此有时它会比ajax更快地提交它。