Creating a dependent dropdown

Hello for all,

I want to use two dropdowns, and one dropdown’s values will be dependent on the value of the other dropdown. please look my code and tell me why not work.

Code _form.php

<div class="simple">

                <?php echo CHtml::activeLabelEx($model, 'company_id'); ?>


                echo CHtml::activedropDownList($model, 'company_id', CHtml::listData(Compagnies::model()->findAll(), 'Id', 'Nom'),


                            'ajax' => array(

                                'type' => 'POST',

                                'url' => CController::createUrl('pro/jrny'),

                                'update' => '#traverse_id',





                <?php echo CHtml::error($model, 'company_id'); ?>


            <div class="simple">

                <?php echo CHtml::activeLabelEx($model, 'traverse_id'); ?>

                <?php echo CHtml::activedropDownList($model, 'traverse_id', array()); ?>

                <?php echo CHtml::error($model, 'traverse_id'); ?>


Code in controller Pro :

 public function actionJrny()



                  array(':_id'=>(int) $_POST['promos_company_id']));


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


//        echo CHtml::tag('option',

//                   array('value'=>$value),CHtml::encode($name),true);

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


please tell me why that’s not work, i need to your helps Thanks

Have a look the CHtml class and find activeDropDownList method.

It is declared:


As we see 4th param of the method gets htmlOptions. but u gave this

<?php echo CHtml::activedropDownList($model, 'traverse_id', array()); ?>

You are not giving id for the dropDown

it should be like that:

<?php echo CHtml::activedropDownList($model, 'traverse_id', array(), array('id'=>'traverse_id')); ?>

You can also do this as


or as




Hope that will help you…

Thanks for your answers but that’s work not !

may be i have error in url : ‘url’ => CController::createUrl(‘pro/jrny’), bcz i didn’t get any result.

we write the url like that : ‘url’ => CController::createUrl(‘pro/jrny’), ?

it is mandatory to use javascript with my code or no ?

i need to your help.

yes, its required