10

Possible Duplicate:
How to manage a redirect request after a jQuery Ajax call

I have a form that is validated client-side with Jquery. Jquery then passes this data to a php script for server-side validation. At the end of the php script, I try to redirect upon success, but no re-direction happens.

Is Jquery interfering with the redirection? How can I get the php script to redirect. Don't want to redirect with jquery, bc I am going to use sessions and want to keep the session data at redirection.

Here is the code:

JQUERY:

$.ajax({
                    //this is the php file that processes the data and send mail
                    url: "includes/process/processAdminLogin.php",
                    //POST method is used
                    type: "POST",
                    //pass the data        
                    data: dataString,    
                    //indicate result is text
                    dataType: "text",
                    //Do not cache the page
                    cache: false,
                    //success
                    success: function (data) {
                        $('input').removeAttr('disabled'); //enable input fields again.

                        if(data == "fail"){
                            $("#statusMessage").html("");
                            $("#statusMessage").html("<p class='statusBoxWarning'>Username and password do not match!</p>");
                            $('button').removeAttr('disabled');
                            document.forms[0].reset();
                        }                               
                    }
                }); 

PHP

if($correctPass == 1){
ob_start();
session_start();
$_SESSION['usernameIdentity'] = $userName;
unset($userName, $userPass);
header("Location: ../../adminDashboard.html");
exit;
}else{
echo "fail";
}

The php script gets to the redirection part and just hangs up. I would really want to keep jquery functionality, along with php redirection. Is there a better method?

Thanks!

                              FINAL WORKING SOLUTION:

Ok, after the input from this post and other similar posts, this is what I have as the working solution. It may not be the most efficient or prettiest solution, but it works, and will have to do for now.

JQUERY

$.ajax({
                    //this is the php file that processes the data and send mail
                    url: "includes/process/processAdminLogin.php",
                    //GET method is used
                    type: "POST",
                    //pass the data        
                    data: dataString,    
                    //indicate result is text
                    dataType: "text",
                    //Do not cache the page
                    cache: false,
                    //success
                    success: function (data) {
                        $('input').removeAttr('disabled'); //enable input fields again.
                        if(data == "success"){
                            $('#statusMessage').html('<form action="http://www.example.com/test/userRegistration/adminDashboard.html" name="userSubscription" method="post" style="display:none;"><input type="text" name="username" value="' + reg_contact_username + '" /><input type="text" name="password" value="' + reg_contact_password + '" /></form>');
                            document.forms['userSubscription'].submit();
                        }else{alert("couldn t redirect");}                              

                    }
                }); 

PHP

if($correctPass == 1){
echo "success";
}else{
echo "fail";
}

The receiving redirection page will have to verify username and password being given again, so validation will be done 2x...which is really inefficient. If anybody can provide me with a better solution - please! A sample write out would also help out.

Thanks!

4

3 回答 3

5

由于 AJAX 发生在“幕后”(可以这么说),您的重定向只会中断对您的 javascript 处理程序的响应。

您需要返回 URL 并让您的回调将浏览器踢到新位置。

更新:

在此说明中,由于您必须将数据返回到前端,因此您需要添加一个status或类似的变量,以便您可以根据调用是否“失败”来切换前端行为。

更新 2:

针对您的解决方案,我强烈建议您反对您的表单提交技术。正如你所说,它效率不高,不漂亮,而且需要两倍的工作(验证用户 2 次)。

如果用户成功登录,为什么不使用 PHP 的内置会话处理来设置会话,然后在简单的 javascript 浏览器重定向后简单地检查会话?

javascript 重定向很简单window.href = "http://mylocation",如果你对 PHP 的会话不太了解,你可以使用我建立的这个类(请忽略它也在管理 cookie,这是一个愚蠢的疏忽)。

于 2013-01-29T03:03:19.237 回答
1

你在哪里刷新你的输出缓冲区?尝试ob_end_flush();header()重定向后添加,看看是否有效。

编辑: 也许将您的相对 URI 转换为绝对 URI 将解决您的问题。这是一个直接从 php.net 提取的通用示例。它基于相对 URI 创建一个绝对 URI——您始终可以对绝对 URI 进行硬编码,而不是这样做。

<?php
  /* Redirect to a different page in the current directory that was requested */
  $host  = $_SERVER['HTTP_HOST'];
  $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
  $extra = 'mypage.php';
  header("Location: http://$host$uri/$extra");
  exit;
?>
于 2013-01-29T03:07:11.473 回答
0

尝试在 AJAX 调用中使用完整的 url。

代替:

    url: "includes/process/processAdminLogin.php",

尝试:

    url: "http://www.mysite.com/includes/process/processAdminLogin.php",
于 2013-01-29T03:04:08.307 回答