[英]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.