Redirect to a page after ajax post problem

hi all,

I don’t really understant why my controller is not redirecting me to my update page…

My will is to redirect to update page corresponding to a row id I checked in admin view. To do this, I create a post ajax request with for data the row id checked, then in my controller function I juste want to go on update page corresponding to the id recieve :

ajax request :


echo CHtml::ajaxLink('<img id="updateImg" src="/myNewApp/images/update.gif" height="15px" width="15px"/>',

                    $this->createUrl('/myapp/redirectUpdateSelection'), 

                      array('type' => 'post',

                        'beforeSend'=>'function(){

                                                  if($.fn.yiiGridView.getSelection("myapp-grid") == "") {

                                                    alert("Pleas select a record");

                                                    return false;

                                                  }

                                                  else if($.fn.yiiGridView.getSelection("myapp-grid").length > 1) {

                                                    alert("Only one record can be edited on same time");

                                                    return false;

                                                  }

                                                  else {return true;}

                                                }',

                        'data' => 'js:{selection:$.fn.yiiGridView.getSelection("myapp-grid")}',

                        'update' => '#output')

                  );

here the controller function :


  public function actionRedirectUpdateSelection() 

  {

    $model=$this->loadModel($_POST['selection']);

    $this->redirect(array('update','id'=>$model->id));

  }

With fire bug I get no error just a post and a get corresponding to the good url… but there is no redirection

thanks

You will have to do the redirect client side because the idea of AJAX is not to cause page reloads. If your action is returning a redirect you will have to follow that one yourself in your JQuery success handler:




if (response.redirect) {

        //REDIRECT

        window.location.href = response.redirect;

}

else {

    //NORMAL RESPONSE

}

Hello

May I know why you haven’t used the standard admin view generated by Gii? It includes in standard an Update button which does well the job, without Ajax.

Besides, what’s the purpose of your ajaxLink really? It seems too much hassle just to redirect to a standard view…

I may be wrong though :)

you cannot redirect bcoz you doing an ajax post as haensel point out you can use client side redirect you can return the url from the action and catch in your view and do a redirect with javascript

My will is to update the row I checked through the check boxes column I added and not using the CbuttonColumn which I don’t really like… But I didn’t succeed to use the variable : $.fn.yiiGridView.getSelection(“myapp-grid”) to build a link like I would like. So I try to use ajaxlink to do it… Indeed there is maybe another way to do it but I didn’t find it, so if you have suggestion to catch the row id that I checked and build a link with it before to redirect from view I’ll get it =D

Ok, where can I implement that exactly ? between my "echo CHtml::ajaxlink" ? or in option of my ajaxlink ?

Is there another way to shap a link in view with variable corresponding to my selection ($.fn.yiiGridView.getSelection("myapp-grid")) than using ajaxlink ?

Ok I found a better and easier way… you pushed me on the good way thanks all, here is the solution, no need to use ajax :


echo CHtml::link( 'updating', '#', array('onclick'=>'js:location.href="'.$this->createUrl('update').'/view?id="+$.fn.yiiGridView.getSelection("myapp-grid");'));