我收到了一个部分构建的网站,并被要求为客户和管理员实现登录系统。我对 JS 和 AJAX 很陌生,我很难理解其他人实现了什么以及如何让它正常运行。
这是登录表单,用户可以在其中输入凭据。
<form name="ajaxform" id="ajaxform" action="auth/ajax-auth.php" method="POST" >
<h4>Client Login</h4>
<div id="error"></div>
<label for="username" class="col-sm-2 control-label">Username</label>
<input id="username" class="form-control" name='username' placeholder="Username">
<label for="password" class="col-sm-2 control-label">Password</label>
<input type="password" class="form-control" id="password" name='password' placeholder="Password">
</div>
<div class="modal-footer">
<button type="button">Close</button>
<button type="submit" id="loginbtn">Login</button>
</div>
</form>
提交表单后,它将运行 ajax-auth.php
并且还将运行 Ajax 函数。然后,它应该根据用户的权限级别将登录到客户端或管理端的用户重定向。
Ajax 函数如下所示:
$("#ajaxform").submit(function(e)
{
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: "auth/ajax-auth.php",
data: dataString,
cache: false,
beforeSend: function(){ $("#loginbtn").val('Connecting...');},
success: function(data){
if(data) {
alert(data + " Logged in!");
if(data == "admin"){
window.location.href = "/admin/admin.php";
}
else if(data == "client"){
//uncomment when client_home.php page is ready
//window.location.href = "client_home.php";
//for now just redirect to homepage
window.location.href = "index.html";
}
} else {
$("#error").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
$("#loginbtn").val('Submit')
}
}
});
}
这是我的 PHP:
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
$username = $_POST['username'];
$password = $_POST['password'];
include '../connection.php';
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($dbc, $sql);
if (mysqli_num_rows($result) == true)
{
$_SESSION["username"] = $username;
$row = mysqli_fetch_array($result);
if ($row["permission"] == 2)
{
$_SESSION["permission"] = 2;
//Redirect to the admin login page.
header("Location: ../admin/admin.php");
}
else if ($row["permission"] == 1)
{
$_SESSION["permission"] = 1;
$_SESSION["userId"] = $row["user_id"];
$_SESSION["account"] = $row["account_id"];
//redirect to the client home page.
header("Location: ../index.html"); //Change this when ready, for now go to homepage
}
}
else
{
echo "User not found!";
header("Location: ../index.html");
}
当我输入用户名和密码(错误或正确)时,什么也没有发生。如果凭据错误,我的错误消息应该会出现,如果我以管理员身份登录,我应该被重定向到管理页面,对于被重定向到客户端页面的客户端也是如此。
我在这里怎么/哪里出错了?
控制台没有向我抛出任何错误,甚至没有显示无效登录凭据的错误消息。
我知道我已经很接近了,非常感谢任何帮助。
最佳答案
您必须在 PHP 中提供一些输出,因为现在您正在重定向请求。这不适用于 AJAX 请求。
例如:
if ($row["permission"] == 2)
{
$_SESSION["permission"] = 2;
// Redirect to the admin login page.
echo 'admin';
}
else if ($row["permission"] == 1)
{
$_SESSION["permission"] = 1;
$_SESSION["userId"] = $row["user_id"];
$_SESSION["account"] = $row["account_id"];
echo 'client';
}
关于javascript - Ajax/PHP 登录没有反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30755686/