ajax and sidebar

i want to use ajax and show the information on the sidebar when a button is clicked.

the side bar is a widget portlet as described in the blog demo.

can i get some help…

You could do something like this.

In model:

<?php echo CHtml::ajaxLink('Approve',array('approve/article'),array(




        'beforeSend'=>'function(){ pThis.append("<br/><span class="gridLinkAjaxing">Approving</span>"); }',

        'success'=>'function(msg){ pThis.replaceWith("<span class="gridLinkAjaxed">Approved</span>"); }',

        'error'=>'function(){ pThis.replaceWith("<span class="gridLinkAjaxFailed">Failed Approving!</span>"); }'),

        array('onclick'=>'var pThis=$(this);')); ?>

And in ApproveController like this:



    * Approve via ajax request


    public function actionArticle()




            // we only allow approving via AJAX request





            throw new CHttpException(500,'Invalid request. Please do not repeat this request again.');


The above is a working example.

Of course, you can change

‘success’=>‘function(msg){ pThis.replaceWith("<span class=“gridLinkAjaxed”>Approved</span>"); }’,
to anything you want, like
'success'=>'function(msg){ $('#sidebarId').replaceWith("<b>New value ;)</b>"); }',

in sidebar: <div id="sidebarId"></div>

if i use thos i donot need any java script file ??

i am new to yii

what is pThis

does this use jquery library

so that i have to download it.

Sorry guy,

Before so many questions did you check the manual? Did you try the solution? Gives you any error?

So first: RTFM!! You can search there for "ajaxLink".

So second: give it a try and see if any error.

I don't think you need to download anything (but Yii).

And "pThis" is JS (JavaScript) and represents the button object clicked (if you study a little jQuery).


any other options other than .replaceWith in


i want to invoke a function in the sidebar to display it

replaceWith() is a jQuery method.

You can't call a php function (of sidebar widget) without reloading the page unless you do this using ajax.

So, what you might do is change url for ajax processing


to your widget url (a method in a controller which runs widget).

Simple echo html from the widget in the controller. And put this html into a div of your html page (inside your sidebar/widget code) with replaceWith(msg).

As you can see,


is a param of 'success'.

If this detailed description doesn't help, then search more about ajax in google.

i have called $this->widget('Display')  Portlet from viws/layouts main.php

so  how do i call it from the ajaxlink button???

Just want to say that the following should be used to replace the above:

        'beforeSend'=>'function(){ pThis.append("<br /><span class=\'gridLinkAjaxing\'>Approving</span>"); }',

        'success'=>'function(msg){ pThis.replaceWith("<span class=\'gridLinkAjaxed\'>Approved</span>"); }',

        'error'=>'function(){ pThis.replaceWith("<span class=\'gridLinkAjaxFailed\'>Failed Approving!</span>"); }'),