Ajax + Yii


I’m TRYING to work with YII and AJAX and I have a question. I’m coding for example a news script and the user can add a news to the already existing news with AJAX. I already sent the informations with a ajaxsubmitbutton to the controller and updated the DB.

Now the question is: What is the best way to add news. Do I have to use append-functions of jquery? How can I add the news to the existing model? Is there a Yii-way?

Important is, that the news can still change by the user (also with AJAX).

And the second question is: How can I debug php when I’m using Ajax requests? Is there any chance for example to see what’s in a variable (like var_dump)?

Thx for help!


  1. Can you write a bit more about structure of view you are adding news on? Is this some kind of yii grid/list view, or perhaps its custom one written by you?

Because if it’s yii widget - let’s say it’s grid - it has to have some Controller action to ask for data (let’s name it actionGetNews). All you have to do is write ajax version of this action (actionAjaxGetNews) and trigger it somehow - the trigger can be success response of action adding the news. The you are doing ajax call to this metod actionAjaxGetNews and update your widget with jQuery method :

	$.fn.yiiGridView.update(componentId, {

		data: dataFromResponse.serialize()


Of course I assume you use Yii widgets. If you dont, then solution will probably be much easier as you don’t have to dig inside all those yii widgets API to figure out how to update components - all you can do with simple jQuery. Of course if you still have problem, paste some of you codes and I can help writing those ajax udates.

  1. When it comes to debugging. Very good is using

Yii::log(print_r($variableToDump), true)

which will log - probably in db, but it depends on settings of your app - dumped variable.

If you have some bigger objects or you want to dump something really huge - like stracktrace - I recommend to just us

file_put_contents(filepath, objecttodump)

  1. You can use jQuery’s functions, but there is a simple wrapper in Yii.

    If your AJAX call returns a HTML response, then you can print it inside the current page.

echo ajaxButton(



    array('replace' => '#this-id')


See CHtml::ajaxButton() and CHtml::ajax().

  1. To debug AJAX, use the developper tools of your brower. They all have a Network tab where you will see the HTTP requests the page sends and the answers it gets.

    Don’t forget you can log JS messages with console.log(myobject); and log from Yii with Yii::app()->log(print_r($myobject, true));. Yii’s log are in protected/runtime/application.log by default.


ok… I’ll try to explain the structure of my application:

It is a Script to show Flights of airplanes (for the University). So I have destinations and trips between the destinations. First I wanted to use a Widget (like the Grid) of YII but the problem is, I have more rows for one data item in the model.


 // It's in a form

echo CHtml::ajaxSubmitButton('Add', array('trip/createdestination', 'id' => '19'), array(

        'type' => 'post',


// end of the form

foreach($model->destinations as $i => $destination) {

   <div class="destination">

   echo $destination->name; // Line 1



   <div class="flight">

   echo $destination->arrival_journey['days']; //line 2 .. It's the flight between the destinations



It just should show the structure that I use. Is there a better way? Is it even possible to realize it with a YII Widget? Is it easier to use a Yii Widget?

With the Ajaxsubmitbutton I send the data to the controller and add the destination and flights to the database. Now I want to ADD the destination and the Flight (if the flight is neccessary) with a fadein to the existing flights. But what is a good way to add the DIVs?

Thx a lot!