How to handle login redirect on an Ajax Post.

hey guys,

Just a quick question. I'm using ajax to submit my forms which are working great, however if a user has been afk from the keyboard for a while and was filling out a form, then return and press submit the login redirect renders in the response div rather then the whole page. Is there a way to stop this? - I tried mucking around in the CWebUser class but had no luck…

Any ideas?




the easiest way seems to be to look for a specific element (e.g. the login form) inside your ajax response.

Take a look at this: http://stackoverflow…query-ajax-call


Hi yoshi,

Thanks for the reply.

Currently I'm using the following code;

<?php echo CHtml::ajaxSubmitButton($update ? 'Save' : 'Create','',array('type'=>'Post','update'=>'#upr',)); ?>

for the submit button, then in the controller;

$this->renderPartial("/ajax/result",array('text'=>'Account updated'));

return true;

after a successfull result.

Any ideas what I need to change/add to get the login redirect to replace the contents of the whole page. I've tried passing it a javascript function to run on success ('success'=>'check_sub') in the submit button however I can never get it to run the js function.



I handled this by utilizing the jquery form plugin to send the ajax request when the form was submitted.

I then called a custom controller to handle the actions and return some json data for sucess or failure.

On success it sends the redirect url along in the response.

for failure i return the errors generated allowing me to put error messages with the field that was wrong and display messages for non field problems.

Hi sn0rcha,

if you want to use the solution presented by the page i linked you can do something like the following.

Button (note: using the success callback disables 'update' - see documentation):

<?php echo CHtml::ajaxSubmitButton($update ? 'Save' : 'Create','',array('type'=>'Post','success'=>'successCallback',)); ?>

Javascript inclusion (you need to give the login form an id - here it is "nigolFrm"):


$loginUrl = $this->createUrl(Yii::app()->getUser()->loginUrl);


function successCallback(data){

        if($("#nigolFrm", data).size() > 0)







// Register the script within yii


The code is just an example, it's not tested.

You should also keep the form's error messages in mind as rfenner pointed out!

Maybe this helps a little bit  ;)