Php Ajax在登录页面中不起作用
我正在学习Ajax,并试图用php创建一个登录页面。我不明白为什么我的代码不起作用。每当我点击login按钮,我的页面就会重定向到另一个页面(例如exam.php),而不检查我的登录功能。我的代码如下。如果有人帮我找出问题,我会很高兴的 HTMLPhp Ajax在登录页面中不起作用,php,jquery,ajax,Php,Jquery,Ajax,我正在学习Ajax,并试图用php创建一个登录页面。我不明白为什么我的代码不起作用。每当我点击login按钮,我的页面就会重定向到另一个页面(例如exam.php),而不检查我的登录功能。我的代码如下。如果有人帮我找出问题,我会很高兴的 HTML <form action="" method="post"> <input name="email" type="text" id="email"> <input name="password" type="passw
<form action="" method="post">
<input name="email" type="text" id="email">
<input name="password" type="password" id="password">
<input type="submit" name="login" id="loginsubmit" value="Login">
</form>
<span class="empty" style="display:none">Field must be empty..</span>
<span class="error" style="display:none">Email or Password not matched !!!</span>
<span class="disable" style="display:none">User Id Disabled.</span>
getlogin.php
$filepath = realpath(dirname(__FILE__));
include_once ($filepath.'/classes/user.php');
$usr = new User();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// this values are coming from main.js
$email = $_POST['email'];
$password = md5($_POST['password']);
$userlogin = $usr->userLogin($email, $password ); // function created in user.php page
}
$filepath = realpath(dirname(__FILE__));
include_once ($filepath.'/../lib/Session.php');
include_once ($filepath.'/../lib/Database.php');
include_once ($filepath.'/../helpers/Format.php');
public function userLogin($email, $password ){
$email = $this->fm->validation($email);
$password = $this->fm->validation($password);
$email = mysqli_real_escape_string($this->db->link, $email);
$password = mysqli_real_escape_string($this->db->link, $password);
if ($email == "" || $password == "" )
{
echo " empty";
exit();
} else {
$query = "SELECT * FROM tbl_user WHERE email = '$email' AND password = '$password'";
$result = $this->db->select($query);
if ($result != false) {
$value = $result->fetch_assoc();
if ($value['status'] == '1') {
echo " disable";
exit();
} else {
Session::init();
Session::set("login", true);
Session::set("userid", $value['userid']);
Session::set("username", $value['username']);
Session::set("name", $value['name']);
}
} else {
echo " error";
exit();
}
}
}
user.php
$filepath = realpath(dirname(__FILE__));
include_once ($filepath.'/classes/user.php');
$usr = new User();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// this values are coming from main.js
$email = $_POST['email'];
$password = md5($_POST['password']);
$userlogin = $usr->userLogin($email, $password ); // function created in user.php page
}
$filepath = realpath(dirname(__FILE__));
include_once ($filepath.'/../lib/Session.php');
include_once ($filepath.'/../lib/Database.php');
include_once ($filepath.'/../helpers/Format.php');
public function userLogin($email, $password ){
$email = $this->fm->validation($email);
$password = $this->fm->validation($password);
$email = mysqli_real_escape_string($this->db->link, $email);
$password = mysqli_real_escape_string($this->db->link, $password);
if ($email == "" || $password == "" )
{
echo " empty";
exit();
} else {
$query = "SELECT * FROM tbl_user WHERE email = '$email' AND password = '$password'";
$result = $this->db->select($query);
if ($result != false) {
$value = $result->fetch_assoc();
if ($value['status'] == '1') {
echo " disable";
exit();
} else {
Session::init();
Session::set("login", true);
Session::set("userid", $value['userid']);
Session::set("username", $value['username']);
Session::set("name", $value['name']);
}
} else {
echo " error";
exit();
}
}
}
我认为问题是因为缺少
User
类定义,所以这个脚本的输出是致命错误,它直接进入ajax调用中的else
语句
在chrome中,您可以使用“网络”选项卡下的开发人员工具检查ajax请求的输出。将按钮类型从提交更改为按钮。签出。另请参见用户类定义的位置?$usr=new User();在getlogin.php页面中添加了它。$usr=new User();在getlogin.php页面中添加了它类本身的定义不应该在user.php中吗?您正在调用类的
userLogin
方法User
,而不在任何地方定义类。类用户{private$db;private$fm;公共函数uu construct(){$this->db=new Database();$this->fm=new Format();}在user.php中添加了它。这里没有提到它。您可以使用chrome开发者工具检查ajax调用的结果吗?