最近我已经面对这个问题了几天。登录验证工作完美。不幸的是,尝试登录后无法重定向到index.php
的另一个页面,并且登录表单正重新加载在表单底部的同一页面上,这应该是错误消息。以防万一用户输入了错误的用户名或密码。这是表格的屏幕截图:
Screenshot of the login form at login.php
Screenshot of the login form when the user doesn't fill all the fields
Screenshot of the login form when the user inputted the wrong username or password
这是我的代码:
login.php
<?php
require_once 'templates/header.php';
?>
<link rel="stylesheet" type="text/css" href="styles/login-style.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#login').click(function(){
var username = $('#usernameID').val();
var password = $('#passwordID').val();
$.post("classes/validation_login.php",{
user_val : username,
password_val : password,
},function(data){
if(data == 'success'){
window.location.href='index.php';
}else{
$('.error-message').html(data);
}
});
});
});
</script>
<title>Login</title>
<form id="login-form">
<h1>Login</h1>
<input type="text" id="usernameID" name="username" placeholder="Username" autocomplete="off"> <br>
<input type="password" id="passwordID" name="password" placeholder="Password" autocomplete="off"> <br>
<input type="button" id="login" name="register-button" value="Login">
</form>
<div class="error-message">
</div>
<?php
require_once 'templates/footer.php';
?>
validation_login.php
<?php
require_once '../core/init.php';
class validation_login{
private $username,$password;
public $errorMessage;
public function validate_login(){
$db = new database();
$this->username = input::get('user_val');
$this->password = input::get('password_val');
if(empty($this->username) || empty($this->password)){
$this->errorMessage = "Please fill all the fields!";
return false;
}else if(!$db->login()){
$this->errorMessage = "Invalid username or password!";
return false;
}else{
session::set('username',$this->username);
return true;
}
}
}
$login = new validation_login;
$login->validate_login();
echo "$login->errorMessage";
?>
任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
您在ajax调用中重定向。多数民众赞成在错误。 删除AJAX调用,然后在表单中添加一个动作标签。 (到validation_login.php)
<form action="validate_login.php" method="post" id="login-form">
答案 1 :(得分:0)
It is because you cannot redirect on the server side when using ajax, try returning a value and redirect base in the value, you can rewrite your code as below
validation_login.php
<?php
require_once '../core/init.php';
class validation_login{
private $username,$password;
public $errorMessage;
public function validate_login(){
$db = new database();
$this->username = input::get('user_val');
$this->password = input::get('password_val');
if(empty($this->username) || empty($this->password)){
$this->errorMessage = "Please fill all the fields!";
}else if(!$db->login()){
return false;
}else{
session::set('username',$this->username);
return true;
}
}
}
?>
login.php
<?php
require_once 'templates/header.php';
?>
<link rel="stylesheet" type="text/css" href="styles/login-style.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#login').click(function(){
var username = $('#usernameID').val();
var password = $('#passwordID').val();
$.post("classes/validation_login.php",{
user_val : username,
password_val : password,
},function(data){
if(data){
//means validation went through
//redirect user
window.location.href='index.php';
}else{
//validation returned false
//display error message
$('.error-message').html('incorrect Username or password');
}
});
});
});
</script>
<title>Login</title>
<form id="login-form">
<h1>Login</h1>
<input type="text" id="usernameID" name="username" placeholder="Username" autocomplete="off"> <br>
<input type="password" id="passwordID" name="password" placeholder="Password" autocomplete="off"> <br>
<input type="button" id="login" name="register-button" value="Login">
</form>
<div class="error-message">
</div>
<?php
require_once 'templates/footer.php';
?>