简体   繁体   中英

PHP and Ajax does not always return a response

I am new in web programming. currently I've been trying to make a login/register website. here is my HTML code:

<!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>

here is my JavaScript code:

$(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);
        }

    }
    );
});
});

and here is my PHP code:

<?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();

?>

The problem here is that if I enter an incorrect login/password, it should alert "No Such Account", but 1 out of 5 attempts it doesn't. First it does, second it does and after third attempt it just doesn't say anything and then after fourth attempt it continues to work fine. Can you give me the reason?

PS I tried to surf every post here and on other forums but none of them seem helpful so if this post is a duplicate please just tell me and I will delete it.

出现此问题的主要原因是,由于没有设置要设置的特定操作和方法,因此它会自动以“ GET”方法提交,因此有时它的提交速度要比ajax快。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM