[英]PHP and Ajax does not always return a response
我是网络编程新手。 目前,我一直在尝试建立一个登录/注册网站。 这是我的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();
?>
这里的问题是,如果我输入了错误的登录名/密码,它应该警告“ No Such Account”,但五分之一的尝试却没有。 首先是,然后是,在第三次尝试后什么都没说,然后在第四次尝试后仍然可以正常工作。 你能给我原因吗?
PS:我试图在这里和其他论坛上浏览每个帖子,但是它们似乎都没有帮助,因此,如果该帖子重复,请告诉我,我将其删除。
出现此问题的主要原因是,由于没有设置要设置的特定操作和方法,因此它会自动以“ GET”方法提交,因此有时它的提交速度要比ajax快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.