Dependent Drop Down

I am creating dependent drop down , I have one main drop down which I fill while loading the view the first drop down is as follows


<div class="row">


	echo CHtml::dropDownList('function_parent','',&#036;this-&gt;getjobtype(),


					'ajax' =&gt; array(








==On selecting one of the options I am populating the second dropdown as follows


<div id="row">

<?php echo CHtml::dropDownList(‘function_id’,’’, array());?>



It works fine , the only thing I am not able to figure out how to keep the second drop down hidden initially , and make it visible when the AJAX call falls back.


I got it solved using success callback instead of ‘update’ , I am associating a javascript function on success callback , and using jquery selectors to change the html of the dependent dropdown, I am not sure if there is a more appropriate way to do this. what I am doing right now is as follows

echo CHtml::dropDownList(‘function_parent’,’’,$this->getjobtype(),

‘ajax’ => array(



‘success’=>'js:function(data){-----JS CODE----}



please share the code for ‘success’=>'js:function(data){-----JS CODE----}

And there’s another request from me if you are kind to share.

If AJAX call falls back it doesn’t keep the 2nd dropdown selected value.

And on update when it loads the form page it happens the same

Might find this useful. Check this link out on dependent dropdowns…

yii dependent dropdown

cheerz! :)

Well, this is exactly what i have on my code and is not working.

Here is solution that worked in my case:


<?php $form->widget('zii.widgets.jui.CJuiDatePicker', 


			'model' => $model,

			'attribute' => 'date',

			'value' => $model->date,

			'options' => array(

					'showButtonPanel' => true,

					'changeYear' => true,

					'dateFormat' => 'yy-mm-dd',


			'htmlOptions' => array(


					'ajax' => array(

						'type'=>'POST', //request type

						'url'=>CController::createUrl('fighters/DynamicFighters'),	//url to call.

						'update'=>'#Matches_id_away',								//selector to update

						'data'=>array('date'=>'js:this.value', 'id_home'=>'js:$("#Matches_id_home").val()'),






more code


<?php Yii::app()->clientScript->registerScript(uniqid(), "

id_away = $('#Matches_id_away').val();



		type	: 'POST',

		url	: '" . $this->createUrl('fighters/DynamicFighters') . "',

		data	: { 'id_home': $('#Matches_id_home').val(), 'date': $('#Matches_date').val() },

		success : function (data) {









public function actionDynamicFighters()


	$homecrt= new CDbCriteria();

	$homecrt->condition	= 'id_fighter = :id_fighter

				AND change_date <= :change_date';

	$homecrt->params	= array(':id_fighter'=>(int) $_POST['id_home'],


	$homecrt->order		= 'change_date DESC';

	$home = FightersUpd::model()->find($homecrt);




		// Ia liga curenta din datele fighterului

		$home = Fighters::model()->findByPk((int) $_POST['id_home']);



	$away = Fighters::model()->with(array(

			'fightersUpds' => array(

				'alias'	=> 'f',

				'select'=> 'id',

				'condition' => 'f.change_date <= :change_date

						AND f.stop_date > :stop_date

						AND f.id_league = :id_league

						AND f.id_fighter != :id_fighter',

				'params' => array(

						':change_date' => $_POST['date'],

						':stop_date' => $_POST['date'],

						':id_league' => (int) $home->id_league,

						':id_fighter' => (int) $_POST['id_home']


				'select'=> 'id,name,surname,id_league',

				'order'=> 'name',



	$away = CHtml::listData($away,'id','FullName');


	echo '<option value="">= Select Away Fighter =</option>';

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


		echo CHtml::tag('option',




Hope it helps someone