php - PHP:如何无法读取函数返回值
问题描述
我想用模式引导和 PHP 和 jQuery 制作登录系统,我在点击登录按钮后显示了这个模式:
<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Login Page</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="exampleInputEmail1">Username</label>
<input type="text" name='username' class="form-control" id="username">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" name='password' class="form-control" id="password">
</div>
<div class="form-group form-check">
<input name='remember' type="checkbox" class="form-check-input" id="remember">
<label class="form-check-label" for="exampleCheck1">Remember me</label>
</div>
<button type="button" name='login' id='login' class="btn btn-primary mr-auto">Login</button>
</div>
</div>
</div>
</div>
所以我有这个依赖于用户类的登录功能:
function login($username ,$password)
{
$database = new Database();
$db = $database->connect();
$user = new User($db);
$user->username=$username;
if($user->check_user()){
$user->get_profile();
if( md5($password)==$user->password){
return 'logined successfully';
} else {
return 'password is wrong';
}
} else {
return 'username is wrong';
}
}
login.php 页面代码是:
<?php
include_once('../includes/functions.php');
//login
if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];
$remember = $_POST['remember'];
$message=login($username ,$password); /// the error is here
if($message=='logined successfully' && $remember =='on'){
$domain = ($_SERVER['HTTP_HOST'] != 'localhost:3308') ? $_SERVER['HTTP_HOST'] : false;
setcookie("username",$username,time()+31556926, '/', $domain, false );
} elseif($message=='logined successfully' && $remember !='on') {
session_start();
$_SESSION['MM_Username'] = $username;
}
}
echo $message;
// end of login
?>
和JavaScript是:
<script>
$(document).ready(function(){
$('#login').click(function(){
var username =$('#username').val();
var password =$('#password').val();
if(document.getElementById("remember").checked==true){
var remember ='on';
} else {
var remember ='off';
}
if(username!='' && password!=''){
console.log(remember);
$.ajax({
url:'login.php',
method : 'POST',
data :{username:username,password:password,remember:remember},
success:function(data){
if(data =='logined successfully'){
console.log(data);
$('#loginModal').hide();
location.reload();
} else {
console.log(data);
$('#loginModal').hide();
alert(data);
}
}
});
} else {
alert('Both username and password required!');
}
})
})
</script>
当我在控制台上记录数据值时,它告诉我来自 login.php 的消息变量未定义,我不知道我的错误在哪里
解决方案
您在第 4 行的帖子变量中使用“登录”索引,但在 jQuery ajax 中,您没有在请求中传递任何登录数据
因此,在您的 php 文件中完成处理后,没有用于回显的 $message 变量,并且您的请求返回为空
将其用作 jQuery ajax 中的数据(我的建议):
data: { login: 1, username: username, password: password, remember: remember },
或者在 php 文件中使用它而不是“isset($_POST['login'])”:
if(isset($_SERVER['REQUEST_SCHEME']) AND $_SERVER['REQUEST_SCHEME'] == 'POST'){
推荐阅读
- r - 为 R 中的某些值计算数据框中的行百分比差异
- javascript - python的json.dumps输出对javascript安全吗?
- javascript - Javascript:将 HSV 转换为 RGB,输出正确但已打乱
- sql - 将 SQL 查询格式化为 XML 文件
- php - Silex:将字符串定义为路由参数的类型提示,它显示字符串不存在
- python - 如何指向 django 将模板放在首页
- python - 无法从网页中获取文本
- javascript - addEvent 类型不匹配 IE8
- c# - C# 等效于 OpenSSL 签名
- python - 如果满足条件,则在 excel 中选择特定行条目并打印整行