1

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;
    } 
opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
Chuksy
  • 43
  • 2
  • 6
  • uhm isn't it target = "_top" ? – opHASnoNAME Feb 21 '12 at 05:08
  • target="_top" and target="_parent" gave me the same result in this instance. The thing is setting this attribute forces all submit actions to the parent, but I'd like to redirect the parent only on successful login. – Chuksy Feb 23 '12 at 15:23
  • BTW, I am using Zend form in a fancybox iframe. Appreciate any help with code examples. – Chuksy Feb 23 '12 at 15:25

2 Answers2

1

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

<!-- Redirect to the parent page, thus closing the fancybox -->
<script type="text/javascript">
    parent.window.location.href = '<?php echo $this->url(/* url params */) ?>';
</script>
Keyne Viana
  • 6,194
  • 2
  • 24
  • 55
  • Do you have any code examples to reference? Alternatively, is it possible to send a successful login variable back to the parent, and the parent can redirect itself if this variable is set? Have been banging my head on ways around this. – Chuksy Feb 23 '12 at 15:20
  • I've added the example. It's more sample than you think. – Keyne Viana Feb 23 '12 at 17:36
  • I ended up doing it another way using AJAX. I'm happy with this ..I'll post it, in case anyone else needs this... – Chuksy Feb 25 '12 at 02:29
0

Here's what I ended up doing:

<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;
    });
});

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'));
        } 

    }
Chuksy
  • 43
  • 2
  • 6