如果语句由于某种原因没有运行,我在 html 中显示 php 的响应时遇到问题。
成功函数无法识别 PHP 回显。
jQuery
<script>
$(document).ready(function(){
$("#login").submit(function(e){
e.preventDefault();
$.ajax({
type:"POST",
url:"logincheck.php",
data:$("#login").serialize(),
success:function(data){
if(data == "Niet oke"){
$("#status").html('Wachtwoord of username onjuist');
}
if(data == "Oke"){
$("#status").html('Mooi');
}
}
});
});
});
</script>
HTML 文件
HTML
<form id="login">
<input type="text" name="username" placeholder="Username" />
<input type="password" name="password" placeholder="Password" />
<input type="submit" name="submit" value="Submit" />
</form>
<span id="status" style="color:black"></span>
*新增logincheck.php文件,新增header建议
PHP
<?php
header("Content-type: application/json");
include_once("includes/dbcon.php");
include_once("pass_system.php");
if(!empty($_POST["username"]) && !empty($_POST["password"])){
$username = $_POST["username"];
$password = $_POST["password"];
$statement = $dbconnect->prepare("SELECT * FROM login WHERE username= :username");
$statement->execute(array(":username" => $username));
while($row = $statement->fetch()){
$salt1 = substr($row["password"], 0, 10);
$salt2 = substr($row["password"], -10);
if(pwhash($password, $salt1, $salt2) === $row["password"]){
header("location: status.php");
echo "Oke";
}
}
}else{
echo "Niet oke";
}
?>
最佳答案
打开您的 Javascript 控制台,查看您的 PHP 脚本如何响应您的 AJAX 调用。
如果可以,我建议您进行 JSON 响应,并在您的 PHP 脚本中放置一个 header("Content-type: application/json")
。
关于php - Jquery AJAX 成功但未给出响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16958562/