refresh content of multiple divs with ajax

Hi All,

Here is the scenario,

I have an ajax link in view and three divs with it like below




    <?php

         ech CHtml::ajaxLink("link",

                              CHtml::normalizeUrl(array('user/ajaxRefreh')),

                              arary(

                                    'type'=>'POST',

                                    'dataType'=>'json',

                                    'success'=>"function(data){

                                                      $('#div1').html(data.div1);

                                                      $('#div2').html(data.div2);

                                                      $('#div3').html(data.div3);

                                                }" 

                              ),     

                              array(

                                    'id'=>'link1',

                              )

      );

    ?>

    <div id="div1">

    </div>


    <div id="div2">

    </div>


    <div id="div3">

    </div>



now in my controller I have this kind of function




     public function actionAjaxRefreh(){

         echo CJSON::encode(

                           array(

                                'div1'=>$this->renderPartial('_view1', array(), false, true),

                                'div2'=>$this->renderPartial('_view2', array(), false, true),

                                'div3'=>$this->renderPartial('_view3', array(), false, true),

                           )

                     );


     }



But when I am running this code its showing the page in html tab and below it written div1, div2, div3 are null

and the divs are not refrehing at all.

Please help me. What am I mising and if its not the correct way, please show me the right path

Note that the third parameter of renderPartial decides if the rendered content will be returned or echoed… you set it to false so no data is returned…

I had done that, but what’s happening is that the targeted div becomes completely blank. Don’t know why… its showing nothing in that div…

Did you check if correct data is returned by the ajax call?

  1. There was not showing any json tab in firebug

  2. Total html tab and response was blank in firebug

  3. div was completely blank after clicking on ajax link

  4. I used exactly these lines in controller




echo CJSON::encode(

                           array(

                                'div1'=>$this->renderPartial('_view1', array(), false, true),

                                'div2'=>$this->renderPartial('_view2', array(), false, true),

                                'div3'=>$this->renderPartial('_view3', array(), false, true),

                           )

                     );




and changed third parameter to true.

The way what I am is that correct or there is some another correct way out?

please help me

So the ajax call is made but there are no data returned.

Try to just


echo "anytext"

instead of the JSON::encode(), just to see if you will get that text as a response…

If you get the text then the problem is in the rendering of your views. Do you need the 4th parameter to be true ?

once I tried this one




echo CJSON::encode(

                           array(

                                'div1'=>"hello"

                           )

                  )



this code refreshed the div with this "hello" but only




$this->renderPartial('_view1', array(), true, true), 



is not working

I tried this one too




$this->renderPartial('_view1', array(), true), 



is also not working

But I need that 4th param true as it contains some ajax links too




echo CJSON::encode(

                   		array(

                                'div1'=>$this->renderPartial('_view1', array(), true, true),

                                'div2'=>$this->renderPartial('_view2', array(), true, true),

                                'div3'=>$this->renderPartial('_view3', array(), true, true),

                   		)

             		);




if that does not work, you must check in your view files if you have stuff there that messes it up.

try to comment out all code in the view files and replace with "div1" "div2" and "div3" in each file,if that works,

then try to add more and more of the code you really want to use in _view1 until

a ) it stops working

b ) it all works

then you will either know that there is no wrong in _view1 or what code in _view1 that makes it stop working.

Then do the same with the other two and then share the code that stops it from working with us and maybe we can help.

Hi all,

Thanks for the solution.

What was happening is that

  1. problem was about third parameter it must be false when you are rendering partially by default but when you are updating using ajax and json it should be true. and then it works like a charm fine.

  2. now my fourth parameter is true always and i can put any ajax link inside it no problem at all.

Thanks

mdomba and Sampa

I’m glad you solved it… but I don’t understand you writing that the problem is the 3rd parameter… when on the #3 post you wrote you tried that already…

No there are two times when i need to do renderPartial

  1. In that div for the first time when the page loads. It means in the main page.

  2. when you are updating the div’s content by calling an action from the controller.

That time I had done the third parameter either (false, false) for both (1,2) situations

or I had done (true, true) for them.

Whereas it should be "false" for situation 1

and “true” for situation 2 in the third parameter’s place.

Thanks for the help.