Yii 1.1: An Easy Solution for Dependent dropDownList Using AJAX


Sometimes new Yii guys face problem to manage dependent dropDownList using AJAX. I am going to discuss an easy solution about this issue.

Example code:

Code in View-

  echo CHtml::dropDownList('region_id','', 
  array(2=>'New England',1=>'Middle Atlantic',3=>'East North Central'),
    'prompt'=>'Select Region',
    'ajax' => array(
    'url'=>Yii::app()->createUrl('YourController/loadcities'), //or $this->createUrl('loadcities') if '$this' extends CController
    'update'=>'#city_name', //or 'success' => 'function(data){...handle the data in the way you want...}',
echo CHtml::dropDownList('city_name','', array(), array('prompt'=>'Select City'));

Code in Controller-

public function actionLoadcities()
   array(':region_id'=>(int) $_POST['region_id']));
   echo "<option value=''>Select City</option>";
   foreach($data as $value=>$city_name)
   echo CHtml::tag('option', array('value'=>$value),CHtml::encode($city_name),true);

I think this will help to understand actually how AJAX works in Yii for dependent dropDownList also AJAX working behavior in Yii framework.

--Enjoy, Explore... Yii

Total 10 comments

#15612 report it
banago at 2013/11/28 06:25am
400 The CSRF token could not be verified

Wondering about the error Yii Error 400 The CSRF token could not be verified...

After some research it turned out that I had to change



'data' => array('region_id' => 'js:this.value', 'YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
#14740 report it
lisseth23 at 2013/09/06 03:42pm

Fue de mucha ayuda!

#14709 report it
freshyiiuser at 2013/09/05 03:48am
cascade Dropdown list one child depend upon multiple perent

this example is applicable in case of one child depends upon multiple parent?

i have cycle, type and behavior, to select the behavior i have to select the cycle and the type.

can anyone help me in this? i didnt find such issue in the forum

thank you

#14371 report it
kalpit at 2013/08/07 02:43am
data is coming in database

nice tutorial... its work for me...

but data is not inserting inside database (only country_id and all, rest all are going inside database).

#13335 report it
skworden at 2013/05/22 02:15pm
Not saving values to db

Did you ever figure out what the problem was from Your own comment on the orginal topic / tip.. This is the same exact thing aka repost with no solution.

#13162 report it
freshyiiuser at 2013/05/09 04:31pm
parent dropdownlist from model

hi, this is the independant dropdown list case, is there an article for dropdownlist from model which get the parent and the child values from the parent?

i need a detailed example, i am new in using yii

thank you

#12370 report it
rkrram37 at 2013/03/16 08:31am
getting error undefined index : region_id

i used the same coding as given above, im getting error undefined index : region_id, help needed

#12221 report it
Adil Shahzad at 2013/03/07 08:07am
Dependent drop down

I've three drop down. 1. Provinces (Hard-codded values) 2. Districts (Filling the drop down using Ajax, working perfect) View Part for the Districts:

<div class="row">
        <?php echo $form->labelEx($model,'inst_province'); ?>
                echo CHtml::dropDownList('inst_province','', 
                array(1=>'A',2=>'B',3=>'C', 4=>'D'),
                  'prompt'=>'Select Province',
                  'ajax' => array(
        <span class="help-inline"><?php echo $form->error($model,'inst_province'); ?></span>            
  1. Tehsils (Problem here, as AJAX does not work here, may be the District drop down ID is not passing) here is the View part
<div class="row">
<?php echo $form->labelEx($model,'inst_distt'); ?>
<?php echo CHtml::dropDownList('inst_distt','', array(), array('prompt'=>'Select Option'),
                        'ajax' => array(
<span class="help-inline"><?php echo $form->error($model,'inst_distt'); ?></span>

Any advice?

#12158 report it
venuk at 2013/03/02 07:28am

It is working fine for display part, I was able to get the first down values using $model but how to save dependent drop down value to $model?

#11966 report it
kevindng at 2013/02/16 02:23am

nice tutorial

Leave a comment

Please to leave your comment.

Write new article