我有一个AJAX脚本,允许用户登录登录页面,但脚本似乎在运行beforeSend后停止。
<script>
$(document).ready(function()
{
$('#login').click(function() {
var username=$("#username").val();
var password=$("#password").val();
var dataString = 'username='+username+'&password='+password;
if($.trim(username).length>0 && $.trim(password).length>0) {
$.ajax({
type: "POST",
url: "login.php",
data: dataString,
cache: false,
beforeSend: function(){
$("#login").val('Connecting...');
},
success: function(data){
if(data) {
$("body").load("<?php echo $dom; ?>").hide().fadeIn(1500).delay(6000);
window.location.href = "<?php echo $dom; ?>";
} else {
$('#shakeme').shake();
$("#login").val('Login')
$("#error").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
}
}
});
} return false;
});
});
</script>
AJAX脚本将数据发送到login.php
if(isset($_POST['username']) && isset($_POST['password'])) {
$username=clean($_POST['username']);
$password=clean($_POST['password']));
$sql = "SELECT password FROM user WHERE username='" . $username . "'";
$result = $conn->query($sql)->fetch_assoc();
$pass = $result['password'];
$salt = getSalt($pass);
$password = $salt . $password;
$password = $salt . hash('sha256', $password);
if(strcmp($pass,$password)==0) {
$_SESSION['login_user']=$username;
}
}
我是否在使用PHP脚本做错了什么?我是不是想以某种方式返回数据?这是我第一次使用AJAX。
答案 0 :(得分:3)
您需要将login.php
文件中的值作为
<强>的login.php 强>
if(strcmp($pass,$password)==0) {
$_SESSION['login_user']=$username;
echo true;
}else{
echo false;
}
exit;
并在 js
内success: function(data){
if (data == true) {
$("body") . load("<?php echo $dom; ?>") . hide() . fadeIn(1500) . delay(6000);
window.location.href = "<?php echo $dom; ?>";
} else {
$('#shakeme') . shake();
$("#login").val('Login')
$("#error") . html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
}
}