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'); ?>

                <?php

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

                        array(

                            'ajax' => array(

                                'type' => 'POST',

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

                                'update' => '#traverse_id',

                            )

                        )

                );

                ?>

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

            </div>

            <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'); ?>

            </div>

Code in controller Pro :


 public function actionJrny()

    {

       $data=jr::model()->findAll('company_id=:_id',

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


    $data=CHtml::listData($data,'id','name');

    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:


activeDropDownList($model,$attribute,$data,$htmlOptions=array())

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




'update'=>'#'.Chtml::activeId($model,'traverse_id'),



or as




'success'=>'function(data){

  $("#'.Chtml::activeId($model,'traverse_id').'").html(data);

}'



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