我真的很困惑为什么我的ajax会起作用。它是关于登录处理。我的开发工具告诉我ajax处理的响应,但是没有执行附加到响应的操作。
我的JS:
$("#button_loginbox").on('click', function() {
login();
});
// Login function
function login() {
// 1. Put values into query variable
var query = {
"username" : $('#login_username').val();
"password" : $('#login_password').val(),
}
// 2. Transmit these value to process_login.php via ajax
$.ajax({
type : "POST",
url : "system/process_login.php",
data : query,
cache : false,
// 3. Use json to transmit multiple values
dataType : 'json',
success : function(html) {
if (html === 'db_error') {
alert('db_error');
// $('#errorbox').html('<p>Database error!!!</p>');
}
if (html === 'failed') {
alert('failed');
//$('#errorbox').html('<p>Login information invalid!!!</p>');
}
// if everything is fine, then inside the process_login.php
// the user will be directed to admin page via header('location:')...
}
}); // /$.ajax
}; // /function login()
我的PHP脚本:
<?php
require_once 'db.php';
session_start();
// Get values sent via ajax and prepare them for database
$username = $db -> real_escape_string($_REQUEST['username']);
$password = $db -> real_escape_string($_REQUEST['password']);
// Hash password
$password_hashed = sha1($password);
// Check if login information is correct
$query = "SELECT * FROM users WHERE username='$username' AND
password='$password_hashed'";
$results = $db -> query($query);
if ($db -> error) {
$response = 'db_error';
echo $response;
}
if (mysqli_num_rows($results) > 0) {
$_SESSION['username'] = $username;
header("location:../admin/admintestpage.php?username=$username");
exit;
} else {
$response = 'failed';
echo $response;
}
?>
*已编辑*
我删除了数据类型命令,现在我得到了响应值 - 然而,当用户输入正确的登录信息时,仍然不起作用的是,他没有被重定向到管理页面。请参阅以下内容:
if (mysqli_num_rows($results) > 0) {
$_SESSION['username'] = $username;
header("location:../admin/admintestpage.php?username=$username");
exit;
} else {
$response = 'failed';
echo $response;
}