dependent dropdown lists

Hi all,

I am trying to create dependent dropdown lists.

Society is the parent model

Apartment is the child to it.

In Apartment table i have 2 dropdowns

society_name(of society table)

block_name(of apartment table)

The value in block_name should be filtered based on the society_name selected.

In the controller i have written

public function actionDynamicstates()


        $data = Society::model()->findAll('Society.society_name=:Apartment.society_name',

                        array(':Society.society_name'=> $_POST['Apartment']['Apartment.society_name']));

        $data = CHtml::listData($Apartment,'block_name','block_name');

            foreach($data as $id => $value)


                echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);



In _form i have written

<div class="row">

		<td><?php echo $form->labelEx($model,'society_name'); ?></td>

	    <?php echo $form->labelEx($model,'Society'); ?>


              $sdata = new CDbCriteria;

              $sdata->order = 'society_name DESC';


       <td> <?php

              echo $form->dropDownList($model,'society_name',CHtml::listData(Society::model()->findAll($sdata),'society_name','society_name'),


                            'ajax' => array(

                            'type' => 'POST',

                            'url' => CController::createUrl('ApartmentController/dynamicstates'),

                            'update' => '#Apartment.block_name',

                         //  'success' =>'hai',

                           // 'error' =>'bai'





        <?php echo $form->error($model,'society_name'); ?>


Some how its not working. help me please

From your browser… check the generated page source… do the block_name input have the id "Apartment.block_name" (I doubt that)…

It’s better to use activeId() for this -

thanks for the reply. The method "dynamicstates" is not being called. I thought there might me some error in this approach.

How do you know it’s not called? Could be that it is called but the returned data is not updated on the current page…

Check with firebug… if it’s called and what it returns…

is this firebug a debugger for yii?

I created the file. every time that i access the page, its not getting updated. I thought if we use ajax function the will get updated. I observed it many times. I thought Dynamicstates funcion is not getting a call.

just google firebug… it’s an addon for firefox… with it you can see if the ajax call has been made… if it was successful… see the parameters that was sent and so on…


maybe this can help you

dependent dropdownlist