我正在使用以下代码在机器上保存图像文件,如果用户是真实的,则保存图像,否则应显示注册页面。但保存图像页面后刷新或验证失败时不重定向注册。有什么问题?
js ajax ::
$.ajax({
type: "POST",
url: "userLoginAction.php",
data: {image: dataURL,uname:uname,pwd:pwd}
}).done(function( respond ) {
alert("Downloading Completed"+respond);
console.log('back on download js**');
});
PHP userLoginAction.php ::
<?php
include("sessionHandler.php");
include("../connect.php");
$con = connectDb();
$db = SelectDB($con);
$username = $_REQUEST['uname'];
$password = $_REQUEST['pwd'];
$img= $_POST['image'];
// To protect MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$query = "SELECT * FROM user WHERE user_email ='".$username."' AND user_password='".$password."'";
$result = mysql_query($query);
$count=mysql_num_rows($result);
if($count==1){
// Register $myusername, $mypassword and redirect to file
createLoginSession($username,$password);
include 'download.php';
}
else {
echo "Wrong Username or Password".$img;
header("location:signup.php");
}
?>
答案 0 :(得分:1)
因为在服务器端进行的重定向不适用于ajax.You必须像你这样成功地进行重定向..试试这样的事情
的Ajax
$.ajax({
type: "POST",
url: "userLoginAction.php",
data: {image: dataURL,uname:uname,pwd:pwd}
}).done(function( respond ) {
if(respond == 'download'){
alert("Downloading Completed"+respond);
console.log('back on download js**');
}
else if(respond == 'login'){
window.location = "url/to/loginpage";
}
});
PHP
include("sessionHandler.php");
include("../connect.php");
$con = connectDb();
$db = SelectDB($con);
$username = $_REQUEST['uname'];
$password = $_REQUEST['pwd'];
$img= $_POST['image'];
// To protect MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$query = "SELECT * FROM user WHERE user_email ='".$username."' AND user_password='".$password."'";
$result = mysql_query($query);
$count=mysql_num_rows($result);
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
createLoginSession($username,$password);
include 'download.php';
echo 'download';
}
else {
echo "login";
}
答案 1 :(得分:0)
试试这个,
<强> AJAX 强>
$.ajax({
type: "POST",
url: "userLoginAction.php",
data: {
image: dataURL,
uname:uname,
pwd:pwd
},
success:function(data) {
if (data == '**DOWNLOAD**') {
alert('Download Completed');
} else if (data == '**LOGIN**') {
window.location.href = 'signup.php';
}
}
});
<强> PHP 强>
include("sessionHandler.php");
include("../connect.php");
$con = connectDb();
$db = SelectDB($con);
$username = $_REQUEST['uname'];
$password = $_REQUEST['pwd'];
$img= $_POST['image'];
// To protect MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$query = "SELECT * FROM user WHERE user_email ='".$username."' AND user_password='".$password."'";
$result = mysql_query($query);
$count=mysql_num_rows($result);
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
createLoginSession($username,$password);
include 'download.php';
echo '**DOWNLOAD**';
}
else {
echo "**LOGIN**";
}
答案 2 :(得分:-2)
要防止在Ajax调用后刷新页面,请在done函数中返回false。