I have three dropdown list that are dependent on each other. Here is the code:
echo CHtml::dropDownList('island_id',$island,$locationsList,
array('ajax'=>array('type'=>'POST',
'url'=>CController::createUrl('supplierHead/getRegions'),
'update'=>'#region_id',),
'id'=>'island_id',
'empty'=>'Choose Island',
));
echo " ";
echo CHtml::dropDownList('region_id',$region, $regionsList,
array('ajax'=>array('type'=>'POST',
'url'=>CController::createUrl('supplierHead/getProvinces'),
'update'=>'#province_id',),
'id'=>'region_id',
'empty'=>'Choose Region',
));
echo " ";
echo CHtml::dropDownList('province_id',$province,$provincesList, array('empty'=>'Choose Province'));
island_id is the parent and when a value is selected, it updates the 2nd dropdown and populates it with data relative to the parent, then once the value for the 2nd dropdown is selected, it populates the available values for the 3rd dropdown.
Problem:
When all drop downs are set with values and I decide to change the value of the first dropdown (parent), it updates the values of the 2nd dropdown, but not the 3rd. The 3rd dropdown is only updated when i manually make a change of a value from the 2nd dropdown. Also, when the 2nd dropdown value is changed, the 3rd dropdown updates which is correct, but the parent does not change. How can I fix this?
Here's my controller:
public function actionGetRegions()
{
$data=Locations::model()->findAll('location_parent=:location_parent AND location_id != :location_parent', array(':location_parent'=>(int) $_POST['island_id']));
$data=CHtml::listData($data, 'location_id', 'location_name');
foreach ($data as $value => $name) {
echo CHtml::tag('option', array('value'=>$value), CHtml::encode($name), true);
}
}
public function actionGetProvinces()
{
$data=Locations::model()->findAll('location_parent=:location_parent AND location_id != :location_parent', array(':location_parent'=>(int) $_POST['region_id']));
$data=CHtml::listData($data, 'location_id', 'location_name');
foreach ($data as $value => $name) {
echo CHtml::tag('option', array('value'=>$value), CHtml::encode($name), true);
}
}
echo CHtml::dropDownList('island_id',$island,$locationsList,
array('ajax'=>array('type'=>'POST',
'url'=>CController::createUrl('supplierHead/getRegions'),
'update'=>'#region_id',),
'id'=>'island_id',
'empty'=>'Choose Island',
));
echo " ";
echo CHtml::dropDownList('region_id',$region, $regionsList,
array('ajax'=>array('type'=>'POST',
'url'=>CController::createUrl('supplierHead/getProvinces'),
'update'=>'#province_id',),
'id'=>'region_id',
'empty'=>'Choose Region',
));
echo " ";
echo CHtml::dropDownList('province_id',$province,$provincesList, array('empty'=>'Choose Province'));
That's all ok you must show your controller file
or else use this
Its on your controller
$data=CHtml::listData($data,'id','name'); //$data depend with the POST id
foreach($data as $value=>$name)
{
echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With