如何仅在***成功***登录时使用PHP(无任何JS)在fancbox iframe中重定向Zend登录表单

如何仅在***成功***登录时使用PHP(无任何JS)在fancbox iframe中重定向Zend登录表单,php,zend-framework,iframe,fancybox,Php,Zend Framework,Iframe,Fancybox,仅使用PHP(即无JS),如何将fancybox iframe中的Zend表单重定向到其父页面(仅当表单成功时)?例如,我在Zend表单中设置了setAttrib(“target”,“_parent”),但它强制提交操作在iframe上重定向,而不是在父页面上重定向。我希望它仅在提交操作失败时返回到父页面。如果登录不成功,它将停留在fancybox iframe上,成功显示错误消息。我正在使用Zend框架。 以下是一些代码: Login.php 登录操作(在authController.php

仅使用PHP(即无JS),如何将fancybox iframe中的Zend表单重定向到其父页面(仅当表单成功时)?例如,我在Zend表单中设置了setAttrib(“target”,“_parent”),但它强制提交操作在iframe上重定向,而不是在父页面上重定向。我希望它仅在提交操作失败时返回到父页面。如果登录不成功,它将停留在fancybox iframe上,成功显示错误消息。我正在使用Zend框架。 以下是一些代码:

Login.php

登录操作(在authController.php内部)


我认为你不能做这种事

在这种情况下,javascript将引用父页面。服务器端无法在标题中设置这些指令

最好是使用引用父页面的javascript指令将页面重定向到另一个页面

使用target属性,它将在任何情况下重定向

例如:

AuthController.php

public function loginAction() {
    // ...
    if(/* is password correct */) {
        //...
        // After a successfull login, just render the view
        $this->render('auth/redirect.phtml');
    }
    // ...
}
重定向.phtml

<!-- Redirect to the parent page, thus closing the fancybox -->
<script type="text/javascript">
    parent.window.location.href = '<?php echo $this->url(/* url params */) ?>';
</script>

parent.window.location.href='';

我认为你不能做这样的事

在这种情况下,javascript将引用父页面。服务器端无法在标题中设置这些指令

最好是使用引用父页面的javascript指令将页面重定向到另一个页面

使用target属性,它将在任何情况下重定向

例如:

AuthController.php

public function loginAction() {
    // ...
    if(/* is password correct */) {
        //...
        // After a successfull login, just render the view
        $this->render('auth/redirect.phtml');
    }
    // ...
}
重定向.phtml

<!-- Redirect to the parent page, thus closing the fancybox -->
<script type="text/javascript">
    parent.window.location.href = '<?php echo $this->url(/* url params */) ?>';
</script>

parent.window.location.href='';

以下是我最后做的事情:

<form id="login"> regular stuff here, elements, etc</form>



<script type="text/javascript">

$(document).ready(function() {

    $('#loginbtn').live('click',function(event){

        var em = $("#useremail").val();
        var pwd = $("#userpassword").val();

        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/auth/login",
            data: { email: em, password: pwd },
            success: function(result){
                if(result.msg == "success"){
                    window.location = "/user/" + result.id + "/home";
                }else{
                    $(".errormsg").html(result.msg);
                }
            },
            error: function(jqXHR, exception) {
                if (jqXHR.status === 0) {
                    alert('Not connect.\n Verify Network.');
                } else if (jqXHR.status == 404) {
                    alert('Requested page not found. [404]');
                } else if (jqXHR.status == 500) {
                    alert('Internal Server Error [500].');
                } else if (exception === 'parsererror') {
                    alert('Requested JSON parse failed.');
                } else if (exception === 'timeout') {
                    alert('Time out error.');
                } else if (exception === 'abort') {
                    alert('Ajax request aborted.');
                } else {
                    alert('Uncaught Error.\n' + jqXHR.responseText);
                }
            }       
        });

        return false;
    });
});
{


下面是我最后做的:

<form id="login"> regular stuff here, elements, etc</form>



<script type="text/javascript">

$(document).ready(function() {

    $('#loginbtn').live('click',function(event){

        var em = $("#useremail").val();
        var pwd = $("#userpassword").val();

        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/auth/login",
            data: { email: em, password: pwd },
            success: function(result){
                if(result.msg == "success"){
                    window.location = "/user/" + result.id + "/home";
                }else{
                    $(".errormsg").html(result.msg);
                }
            },
            error: function(jqXHR, exception) {
                if (jqXHR.status === 0) {
                    alert('Not connect.\n Verify Network.');
                } else if (jqXHR.status == 404) {
                    alert('Requested page not found. [404]');
                } else if (jqXHR.status == 500) {
                    alert('Internal Server Error [500].');
                } else if (exception === 'parsererror') {
                    alert('Requested JSON parse failed.');
                } else if (exception === 'timeout') {
                    alert('Time out error.');
                } else if (exception === 'abort') {
                    alert('Ajax request aborted.');
                } else {
                    alert('Uncaught Error.\n' + jqXHR.responseText);
                }
            }       
        });

        return false;
    });
});
{


嗯,这不是target=“\u top”?target=“\u top”和target=“\u parent”在这个实例中给出了相同的结果。设置此属性会强制所有提交操作都提交给父级,但我只想在成功登录时重定向父级。顺便说一句,我正在fancybox iframe中使用Zend表单。感谢您对代码示例的帮助。嗯,这不是target=“\u top”?target=“\u top”和target=“\u parent”在此实例中给出了相同的结果。设置此属性会强制将所有提交操作提交给父级,但我只想在成功登录时重定向父级。顺便说一句,我正在fancybox iframe中使用Zend表单。感谢对代码示例的帮助。您有任何代码示例可供参考吗?或者,是否可以发送一个成功的登录变量返回到父级,并且如果设置了这个变量,父级可以重定向自己?我一直在想方设法解决这个问题。我添加了这个示例。它比你想象的更简单。我最终使用AJAX以另一种方式完成了它。我很高兴。我会发布它,以防其他人需要它…你有代码检查吗请参考?或者,是否可以将一个成功的登录变量发送回父级,并且父级可以在设置此变量后重定向自身?我一直在想方设法解决这个问题。我添加了这个示例。它比您想象的更简单。我最终使用AJAX以另一种方式完成了它。我很高兴看到这一点。我将在中发布它万一有人需要这个。。。
    $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(TRUE); // suppress auto-rendering and displaying a view
    $data['email'] = $_POST['email'];
    $data['password'] = $_POST['password'];

    if ($this->_processLogin($data)) {
            // We're authenticated!
        $auth = Zend_Auth::getInstance();
        $id = $auth->getIdentity()->id;
        echo Zend_Json::encode(array('msg' => 'success', 'id' => $id));
    } else {
                  $this->_helper->json(array('msg' => 'Invalid credentials. Try again'));
        } 

    }