Im trying to create a dependent dropdown, but I can’t get it to work.
Here is my code:
_search:
<?php
echo $form->DropDownList($model,'country', array(1=>'Sweden',2=>'Finland',3=>'Denmark'),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('CountryController/dynamiccity'),
'update'=>'#'.CHtml::activeId($model,'city')
)));
?>
<?php echo $form->dropDownList($model,'city',array()); ?>
CountryController:
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','dynamiccity'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
public function actionDynamiccity() {
$country_id = $_POST['country'];
$data=Citys::model()->findAll('country_id=:country_id',
array(':country_id'=> $country_id));
$data=CHtml::listData($data,'city_id','city_name');
foreach($data as $value=>$city_name) {
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($city_name),true);
}
}
I have looked at these posts
http://www.yiiframework.com/wiki/24/
There is no error provided but the second dropdown is empty no matter what I choose in the first dropdown.
Anyone see any errors in the code?
Do I need to activate-functionallity ajax somewhere?