How to redirect Zend login form in a fancbox iframe with PHP (without any JS) only on ***successful*** login-Collection of common programming errors

Using only PHP (i.e. no JS), how do you redirect a Zend form in a fancybox iframe to its parent page, only if the form was successful? For instance, I set setAttrib(“target”,”_parent”) in the Zend form, but it forces the submit action to redirect on the iframe, and not the parent page. I want it to return to the parent page only if the submit action is unsuccessful. If login is not successful, it stays on the fancybox iframe successfully displaying error message. I am using Zend Framework. Here are some code:

Login.php

class Application_Form_Login extends Zend_Form
{

    public function init()
    {
        $this->setAction('/auth/login')
             ->setAttrib('id', 'login_form')
             ->setAttrib('target', '_parent')
             ->setName('login_form')
                     ->setMethod('post');

    ----->added all elements here
    }

}

loginAction (inside authController.php)

    public function loginAction()
    {

        $request = $this->getRequest();
        $form = new Application_Form_Login();
        //$form->setAttrib('target','_parent');
        //$this->view->login_form = $form;

        if($request->isPost()){
            if($form->isValid($request->getPost())){
                $data = $form->getValues();

                if ($this->_processLogin($data)) {
                    // We're authenticated! Redirect to the home page
                    $auth = Zend_Auth::getInstance();
                    $id = $auth->getIdentity()->id;
                        //this is where I need some logic to redirect on parent page only if successful login 
                    $this->_redirect('/user/'.$id.'/home');  
                } else {
                    $form->setAttrib('target','_self');
                    $this->view->errorMessage = "Invalid credentials. Try again";
                    $form = new Application_Form_Login();
                    $this->view->login_form = $form;
                }         
            }else{
                    $form->setAttrib('target','_self');
                    $this->view->errorMessage = "Can't be empty. Please try again.";
                    $form = new Application_Form_Login();
                    $this->view->login_form = $form;
            }
        }else{
        }

        $this->view->login_form = $form;
    } 
  1. Here’s what I ended up doing:

     regular stuff here, elements, etc
    
    
    
    
    
    $(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;
        });
    });
    

    And in my controller action:

    public function loginAction()
    

    {

        $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'));
            } 
    
        }
    
  2. I think you can’t do such thing.

    The javascript would reference the parent page in that case. The server side has no way to set in the headers those instructions.

    The best you can do is to redirect the page to another one with the javascript instruction referencing the parent page.

    Using the target attribute it will redirect in any case.

    Example:

    AuthController.php

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

    redirect.phtml

    
    
        parent.window.location.href = '';