After an ajax request i got one array from my controller as response, how can I use this array values in my dropdown list?
Answer
in my view I have
echo CHtml::dropDownList('client_id', '',CHtml::listData($model,'client_id','client_name'), array(
'ajax'=> array(
'type'=>'POST',
'url'=>Yii::app()->baseUrl.'/index.php?r=page/dynamicDropdownList',
'update'=>'#program_id',
'empty'=>'-Select a Client-')));
// I need to populate the response array in this dropdownlist
echo CHtml::dropDownList('program_id','', CHtml::listData($result,'program_id', 'program_name'));
in my controller
public function actionDynamicDropdownList()
{
if($_POST['client_id'] > '0') {
$result = Yii::app()->db->createCommand()->select('program_id, program_name')->from('program')->where('client_id='.$_POST['client_id'].'')->order('program_name')->queryAll();
$this->render('admin', array(
'result' => $result,
));
}
}
Another Problem
Now I got everything working except the second drop downlist is also showing the values of 1st dropdown list with the result.
solution:- I have parse the response and show it in the dropdownlist
This is how your controller should look like:
You catch the data from ajax or GET and you do something with it, then you pas the data to the view;
And this should be what the vies contains:
You have to work with data received using ajax in the controller, as it is not visible in the view, if it was not passed using render or renderpartial