我想知道在通过php中的Ajax调用成功登录后如何重定向url?请检查我的代码我在做什么错。
下面是网址http://localhost:8080//myproject/adminseller/registration.php上的registration.php文件:
<script>
function createLogin () {
//alert("Hello! I am an alert box!!");
var data = {
'email' : jQuery('#email').val(),
'password' : jQuery('#password').val(),
}; // data obect End
//Ajax call Start Here
jQuery.ajax({
url : '/myproject/adminseller/login.php',
method : 'POST',
data : data,
success : function(data){
//alert(data); // For checking Code in alert
if (data != 'passed') { // passed is else statement in
check_address.php when no errors are showing
jQuery('#modal_errors_1').html(data);
}
if (data == 'passed') {
//alert('passed!');
//clear the errors if any
jQuery('#modal_errors_1').html("");
location.reload();
}
},
error : function (){alert("Something went wrong.");},
});
}
</script>
下面是Login.php文件
<?php
if (isset($_POST['email'])) {
$email = sanitize($_POST['email']);
}
if (isset($_POST['password'])) {
$password = sanitize($_POST['password']);
}
// echo $email; // Output x2a0889@gmail.com
// echo $password; // 123456
// Email and password matched and no erros so I am removing validations code from here, So else condition will execute here
// Check for Errors
if (!empty($errors)) {
echo display_errors($errors);
}else{
// Assume here login is successful
echo 'passed';
**// Here is the problem via Ajax Call url is not redirecting after login successful, and always getting url is: http://localhost:8080//myproject/adminseller/registration.php **
header('Location:index.php');
}
?>
我的问题是,成功登录后,URL仍然没有更改 http://localhost:8080//myproject/adminseller/registration.php
我想重定向页面http://localhost:8080//myproject/adminseller/index.php
成功登录后。
任何想法或建议都会受到欢迎。
答案 0 :(得分:0)
您需要使用window.location.href而不是location.reload();
<script>
function createLogin () {
//alert("Hello! I am an alert box!!");
var data = {
'email' : jQuery('#email').val(),
'password' : jQuery('#password').val(),
}; // data obect End
//Ajax call Start Here
jQuery.ajax({
url : '/myproject/adminseller/login.php',
method : 'POST',
data : data,
success : function(data){
//alert(data); // For checking Code in alert
if (data != 'passed') { // passed is else statement in
check_address.php when no errors are showing
jQuery('#modal_errors_1').html(data);
}
if (data == 'passed') {
//alert('passed!');
//clear the errors if any
jQuery('#modal_errors_1').html("");
window.location.href = "http://localhost:8080//myproject/adminseller/index.php";
}
},
error : function (){alert("Something went wrong.");},
});
}
</script>
Below is the Login.php File
<?php
if (isset($_POST['email'])) {
$email = sanitize($_POST['email']);
}
if (isset($_POST['password'])) {
$password = sanitize($_POST['password']);
}
// echo $email; // Output x2a0889@gmail.com
// echo $password; // 123456
// Email and password matched and no erros so I am removing validations code from here, So else condition will execute here
// Check for Errors
if (!empty($errors)) {
echo display_errors($errors);
}else{
// Assume here login is successful
echo 'passed';
**// Here is the problem via Ajax Call url is not redirecting after login successful, and always getting url is: http://localhost:8080//myproject/adminseller/registration.php **
header('Location:index.php');
}
?>
答案 1 :(得分:0)
您当前正在重定向AJAX请求。
对于成功的AJAX请求,您不能从PHP端进行重定向。
不过,您可以从Javascript捕获返回的值,然后从那里重定向。
而不是在PHP脚本中调用标头位置,而是将其放在if语句中,以检查响应是否为passd
window.location.href = 'index.php';
这将导致浏览器转到index.php
。
答案 2 :(得分:0)
您正在Ajax调用中重定向,事实并非如此。请从PHP代码中删除以下行:
** //这是通过Ajax出现的问题,登录成功后,呼叫url不重定向,并且始终获取url是: http://localhost:8080//myproject/adminseller/registration.php ** header('Location:index.php');
然后替换下面的JS代码:n。
///如果有任何jQuery('#modal_errors_1')。html(“”);清除错误;
location.reload();
使用以下代码:
///如果有任何jQuery('#modal_errors_1')。html(“”);清除错误;
window.location.href ='index.php';