Yii Ajax problem


I have following problem and I don’t know how to solve it.

I have table with lots of rows that can be dynamically added/removed by jQuery. It works. But on each row of this table there are 2 drop-down-lists. If I choose something in the first one, the second one should be refreshed and filled with new values using ajax. Typical task. But it does not work as I want.

Because all rows have the same structure I created a function that creates code for a new row. This function is in controller. If I use it, Yii does not generate jQuery code and my ajax does not work.

If I try to define one set of drop-down-lists manually in the view, it works well.

I wanted not to use the Yii’s ajax and to write it by my self. It is easier than the Yii’s way, but it does not work neither. I did it like this:

echo CHtml::dropDownList(






this produces following HTML code:

select id="name" name="name" onchange="$.post('/mysite/controller/action');">

And when I select a value in the drop-down-list I only get CHTML error 400 = bad request?

The post() function does not work.

Im not able to describe my problem better, it is very complacated … I hope someone has solved a similar problem…

Try to launc the script alone:


Maybe you need


(*without the first slash that means http://www.yoursyte.com/mysite/controller/action)

But in this way, you dont send anything: if you want to send data you can use some ways:

$.post("test.php", { name: "John", time: "2pm" } );

or also

$.post("test.php", $("#testform").serialize());

(you can find more documentation at http://api.jquery.com/jQuery.post/)

Hi. Thanks for response. I already tried all you mentioned but it did not work. I really do not know where can be the problem.

On the internet I found that error 400 =

The request could not be understood by the server due to malformed syntax

But when I open Firebug I see that script calls existing action and if I enter it’s address to browser, it works.

Now I found anothet error message:

The CSRF token could not be verified.

It maybe an error due to some safety in Yii.

I will have look at it.

My drop down box looks like this. It has to be in form to be able to send POST variable:

<form method="post" action="/website/controller/action">

  <div style="display:none">

    <input type="hidden" name="YII_CSRF_TOKEN" value="xxx">


  <select id="mySelect" name="mySelect" onchange="$.post('/mysite/controller/action');">

    <option value="1">has value I</option>

    <option value="2">has value II</option>



I tried to add to form attribute:


… but no change.

Does this help?



Tadaaa !! Success

This error occured because I turned form-security settings on in protected/config/main.php:






Every form than has to contain (and automatically contains) this field:

<input type="hidden" name="YII_CSRF_TOKEN" value="xxx">

Problem is that if you want to send something via POST, you have to send also this field.

In case of $.post() you MUST add at least one variable (named YII_CSRF_TOKEN) like this:


// mysite = Yii::app()->request->baseUrl

// xxx = Yii::app()->request->csrfToken

If you do it like this, you do not need to use a form and serialize it. Yii’s Ajax puts it in a form and serializes it because of the YII_CSRF_TOKEN field.

Thats what I wrote^^


Actually… The main problem was not solved. Yii’s default Ajax doesn’t work. Now works only my manual approach.

When I use Yii’s ajax (drop down list) it works only if there is not defined any <script> section the same view.

Does anybody know the reason?

PS: And what’s funny - if the drop down list created using Yii is present on the page, no java scripts work at all.

Please post the generated source that does NOT work.

So when I was preparing the code for you, I foud out that when I was specifying "src" for js:

<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/ ... " ></script>

… i didn’t add a “dot” infront of path. Because Yii::app()->request->baseUrl returns “/website/” and Yii’s Ajax than crashed. So I added the dot like this:

<script type="text/javascript" src=".<?php echo Yii::app()->request->baseUrl; ?>/ ... " ></script>

But firebug marked it as a mistake.

Second thing is something I still do not understand. I have a text box that should be driven by "datetimepicker" like this:

$("#textboxId" ).datetimepicker({

// properties


But browser does not like the .datetimepicker

If I remove the Ajax drop down list, everything works well…

And script added by Yii’s ajax looks cca like this. (I reformated the code to be more readable) Online validator says that there is missing a semicolon. I am not sure.

<script type="text/javascript">

















           return false;





Yii adds this code to the end of body. If I open generated HTML source of my webpage and copy this script to the top of my view (above all <script> sections), ajax works. But still the rest of javascript don’t.

Yii’s Ajax sucks I think… I will do I my way.