AJAX and Yii

I’ve been all over the documentation, and am still missing the basics of how AJAX gets handled in Yii. I have an application that ideally will use AJAX for database updates/reads, otherwise the updates will happen as usual without JS. I’ve got things working without AJAX, but can’t seem to get it working with AJAX - missing something basic. So here’s my questions:

  1. How do Yii’s AJAX methods degrade? Does a form just cease to submit, or does it submit along with a page refresh?

  2. Would anybody be able to share their code using basic AJAX with me ;) ? While testing question 1, I couldn’t seem to get the AJAX working in the first place. If I saw a basic example, that should solve the problem. Or is there a tutorial or example anywhere in the documentation I missed?


I don’t have a full code. But this will give you basic idea.

You will need first of all, jquery included. The best efficient way is to use google api.

This way…


  echo CGoogleApi::init();

  echo CHtml::script(CGoogleApi::load('jquery', '1.3.2') . "\n" .

                     CGoogleApi::load('jquery.ajaxqueue.js') . "\n" .

                     CGoogleApi::load('jquery.metadata.js') . "\n" 



This way, your jquery from google CDN will be included.




   '$(".info").animate({opacity: 1.0}, 3000).fadeOut("slow");',




With these lines of code we register a piece of jQuery (already included with YII) javascript code, using ‘myHideEffect’ as ID. It will be inserted in the jQuery’s ready function (CClientScript::POS_READY). Due to the chainablity of jQuery the little script will run two effects on the .info DIV sequentially:

Link to above snippet

To use ajax requests, check out the api. There is a class CHtml having a method ajax.

For my Ajax stuff I’m not using the yii Ajax at the moment, mostly since I didn’t notice it was there.

What I did was to create a controller that responds to the Ajax calls process the request then sends back some json for the success function to process.

So with the controller you set up your Ajax calls to call it with the approriate action and then have the success callback process the response sent back.

Id give you some code but I’m not in front of my computer.

Thanks for the responses.

I’ve got jQuery included with no problem, I’m using it in a photo gallery. Using the CHtml ajax methods doesn’t seem to be getting information to the controller, however. Is there something different from the normal that I need to be doing in the controller to get the information? I searched the forum and came across a few threads that seemed to suggest that, yet I couldn’t find what it was different that I needed to be doing.

Can you see the GET/POST request in Firebug console?

In the controller, you can check isAjaxRequest if you don’t want the action to be called directly. Besides that, nothing special for the request to reach the controller action.

I’m still new to the alternatives for data passing (text, Json, …), so I refrain from trying to elaborate on that topic.