我有一个PHP脚本,通过他们的电子邮件和密码验证用户,如果它无效,我的脚本回应“无法登录”之类的内容,如果成功,它将指向另一个页面,我该怎么做?
这是我的PHP代码:
if(isset($_POST['email'])&&isset($_POST['password'])){
$email = $_POST['email'];
$password = $_POST['password'];
$result = userAuthentication($email,$password);
if($result == false){
echo 'Unable to login';
}
else if($result == true){
header("location: success.php");
}
}
这是我的js代码:
$(function() {
$("button").button();
$("#clickme").click(function(){
$.post("check.php", {
"email": $("#txtEmail").val(),
"password": $("#txtPassword").val()
},
function(msg){
$(".message").html(msg);
});
return false;
});
});
答案 0 :(得分:2)
您无法像这样从PHP重定向。您可以返回成功消息并从javascript重定向:
PHP:
if(isset($_POST['email'])&&isset($_POST['password'])){
$email = $_POST['email'];
$password = $_POST['password'];
$result = userAuthentication($email,$password);
if($result == false){
echo 'Unable to login';
}
else if($result == true){
echo 'success';
}
}
的javascript:
$(function() {
$("button").button();
$("#clickme").click(function(){
$.post("check.php", {
"email": $("#txtEmail").val(),
"password": $("#txtPassword").val()
},
function(msg){
$(".message").html(msg);
if(msg == 'success'){
window.location = 'success.php';
}
});
return false;
});
});