Dependent Dropdown List Box

Hi All,

I know they are many post on this query. But, still i am unable to do so posting this.

I want to create a dependent dropdown list box.

I have City table as well as Location table. These are two dropdown list box in my User creation form.

As city is selected , location should get filtered. I have tried following thing

_form.php i have this




<?php

/* @var $this UserController */

/* @var $model User */

/* @var $form CActiveForm */

?>


		

		<?php 

		$city_model = City::model()->findAll(array('order' => 'CITY_NAME')); 

                $list = CHtml::listData($city_model, 'CITY_ID', 'CITY_NAME');    

                echo $form->dropDownList($model, 'RCity_ID', $list);       

               

		array(

			'prompt'=>'Select City',

			'ajax' => array(

			'type'=>'POST', 

			'url'=>CController::createUrl('loadlocation'),

			'update'=>'#RLocation_ID', 

			'data'=>array('RCity_ID'=>'js:this.value'),

			));

		?>




	

		<?php 

		echo CHtml::dropDownList('RLocation_ID','', array(), array('prompt'=>'Select Location'));

		?>




In User Controller i have this




public function actionLoadlocation()

	{

		$data=Location::model()->findAll('CITY_ID=:RCity_ID', 

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

 

		$data=CHtml::listData($data,'location_id','location_name');

 

		echo "<option value=''>Select City</option>";

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

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

	}

}






my dbtable structure is as follows

Table Name: city

columns: 1)CITY_ID 2)CITY_NAME

Table :location

column: 1)Location_ID 2)Location_Name 3)City_ID

Thanks in advance

In the first part, I noticed the options aren’t include in the dropDownList.

Supposed to be:




<?php 

                $city_model = City::model()->findAll(array('order' => 'CITY_NAME')); 

                $list = CHtml::listData($city_model, 'CITY_ID', 'CITY_NAME');    

                echo $form->dropDownList($model, 'RCity_ID', $list, array(

                           'prompt'=>'Select City',

                           'ajax' => array(

                              'type'=>'POST', 

                              'url'=>CController::createUrl('loadlocation'),

                              'update'=>'#RLocation_ID', 

                              'data'=>array('RCity_ID'=>'js:this.value'),

                           )

                        )

                     );

                ?>




Thanks a lot Compact_corpse…

It finally worked… :)