I have done so, following is my View’s code
<?php echo $form->labelEx($model,'Country_Id'); ?>
<?php echo $form->dropDownList($model,'Country_Id', CHtml::listData(Country::model()->findAll(), 'Country_Id', 'Name'),
array('empty'=>'Select Country',
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('Beneficiary/dynamicprovinces'),
//'update'=>'#'.CHtml::activeId($model, 'Province_Id') ))
'dataType' => 'json',
'data'=>array('Country_Id'=>'js:this.value'),
'success'=>'function(data) {
$("#Province_Id").html(data.dropDownProvinces);
$("#City_District_Id").html(data.dropDownCityDistricts);
}',
)
)); ?>
<?php echo $form->error($model,'Country_Id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Province_Id'); ?>
<?php echo $form->dropDownList($model,'Province_Id', array(),
array('empty'=>'Select Province',
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('Beneficiary/dynamiccitydistricts'),
'update'=>'#'.CHtml::activeId($model, 'City_District_Id'))
)); ?>
<?php echo $form->error($model,'Province_Id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'City_District_Id'); ?>
<?php echo $form->dropDownLIst($model,'City_District_Id', array(),array('empty'=>'Select City/District') ); ?>
<?php echo $form->error($model,'City_District_Id'); ?>
</div>
And following is controller’s code
public function actiondynamicprovinces(){
$data=Province::model()->findAllBySql('select Province_Id, Name from mts_Province where Country_Id=' .$_POST['Beneficiary']['Country_Id']);
$data=CHtml::listData($data, 'Province_Id', 'Name');
$Provinces = CHtml::tag('option', array('select'=>'selected'), CHtml::encode('Select Province'), true);
foreach($data as $value=>$name){
$Provinces .= CHtml::tag('option', array('value'=>$value), CHtml::encode($name), true);
}
$data=CityDistrict::model()->findAllBySql('select Province_Id, Name from mts_City_District where Province_Id=' .$_POST['Beneficiary']['Province_Id']);
$data=CHtml::listData($data, 'City_District_Id', 'Name');
$CityDistricts = CHtml::tag('option', array('select'=>'selected'), CHtml::encode('Select City or District'), true);
foreach($data as $value=>$name){
$CityDistricts .= CHtml::tag('option', array('value'=>$value), CHtml::encode($name), true);
}
echo CJSON::encode(array(
'dropDownProvinces'=>$Provinces,
'dropDownCityDistricts'=>$CityDistricts
));
}
public function actiondynamiccitydistricts(){
$data=CityDistrict::model()->findAllBySql('select Province_Id, Name from mts_City_District where Province_Id=' .$_POST['Beneficiary']['Province_Id']);
$data=CHtml::listData($data, 'City_District_Id', 'Name');
foreach($data as $value=>$name){
echo CHtml::tag('option', array('value'=>$value), CHtml::encode($name), true);
}
}
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', 'dynamicprovinces', 'dynamiccitydistricts'),
'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('*'),
),
);
}
But the problem is that country drop down populated, when i select a conutry, province and city_district drop downs does not populate.
Can you please help