Redirect with Ajax

Published on and tagged with ajax  cakephp  javascript  tip

In my current project I have a login form I want to validate with Ajax. If everything is ok, the user should be redirected to the respective start page. As it seems to be impossible to do the redirect on the server-side (at least to me), I do it on the client-side. I use the following snippet in my view:

<div id="login">
    <?php echo $ajax->form(array('action' => '/login'), 'post', 
                                              array('update' => 'login')); ?>
    ...
    </form>
    <?php if (isset($url)) echo $javascript->codeBlock('window.location = "'.$url.'"'); ?>
</div>

And in my controller I use the following code:

if ($this->RequestHandler->isAjax())
{
    $this->set('url', '/'.$page);
}
else
{
    $this->redirect($page);
}

8 comments baked

  • Nate

    If you are redirecting within the same controller, you can use Controller::setAction( ). You pass it the name of the action you want to use, along with any parameters to pass it.

    Just remember to return after calling it, so your current action does not continue.

  • cakebaker

    @Nate: Thanks for the hint. For the current form I cannot use this function as I redirect to other controllers, but maybe in another situation that function would be helpful.

  • c_t

    For me, in most cases

    $this->redirect(‘/ajax/controller/action’);

    (e.g. $this->redirect(‘/ajax/users/index’); ) works fine. Of course only the content inside the that is updated by Ajax is reloaded.

    If you need a complete site-reload or -relocation the suggested javascript-method seems to be needed.

    Thanks anyway for the tip, I’m using it!

  • sunertl

    i figured out this pretty simple way: in the controller action i use the following code:

    if ($this->RequestHandler->isAjax())
    $this->Session->write(‘ajax’, 1);
    $this->redirect(‘/where/you/want’));

    in the app_controller i add the following to beforeRender:

    if($this->Session->read(‘ajax’) == 1) {
    $this->layout = ‘ajax’;
    $this->Session->write(‘ajax’, 0);
    }

    for me it works pretty good, is done on the server-side and requires only a few lines of code.

  • sunertl

    ok, after i posted the first comment, i discovered an even better way. i override the default redirect() in the app_controller by adding:

    function redirect($url, $status = null) {
    if ($this->RequestHandler->isAjax()){
    $this->Session->write(‘ajax’, 1);
    }
    parent::redirect($url, $status);
    }

    in beforeRender the code remains:

    if($this->Session->read(’ajax’) == 1) {
    $this->layout = ‘ajax’;
    $this->Session->write(’ajax’, 0);
    }

    using this way doesn’t even require you to change any code written already :-).

  • cakebaker

    @sunertl: Thanks for sharing that tip :)

  • cakebaker » A simple redirect component

    […] I already wrote about doing a redirect with Ajax. Now, I created a simple component from that code I presented there: […]

  • Get Started With AJAX in CakePHP « Ahsan’s Laboratory

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License