如果语句由于某种原因没有运行,我在我的 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";
}
?>