I know this question might be familiar to most of you, but allow me to ask because most of the answers to related questions here in this forum and in YII forum have not been given a satisfactory answer. My two depended drop downs are working fine and the 2nd one gets populated based on the selection of the first one. All values are from the database. Now my problem is that, the second dropdown keeps on returning an empty value even after a value is selected and it doesn't seem to be validated. I have searched through the forums but none of them seem well tackled. Pliz help me out. This very important to me.
EDITED: I am sorry, I had assumed that its sth that u already know but here is my code: _form.php
<div class="row">
<?php echo $form->labelEx($users,'country_id'); ?>
<?php echo $form->dropDownList($users,'country_id',CHtml::listData(Country::model()->findAll(
array('order' => 'country')),'country_id','country'),
array(
'class'=>'form-control',
'prompt'=>'Choose Your Country',
'ajax'=>array(
'type'=>'POST',
'url' => CController::createUrl('users/findcity'),
'data'=> array('country_id'=>'js:this.value'),
'update'=>'#city_id'))
);?>
<?php echo $form->error($users,'country_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($address,'city_id'); ?>
<?php echo CHtml::dropDownList('city_id','', array(),
array('prompt'=>'Choose Your City','class'=>'form-control')); ?>
<?php echo $form->error($address,'city_id'); ?>
</div>
And here is my controller function that find the cities associated with the choosen country
public function actionFindcity()
{
$data = City::model()->findAll('country_id=:country_id',
array(':country_id'=>$_POST['country_id']));
$data=CHtml::listData($data,'city_id','name');
echo "<option value=''>City</option>";
foreach($data as $value=>$name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
}
I have tried many ways but I cant seem to figure out where the problem is, tho am doubting these line of code coz I cant seem to find the model that responds to this code:
<?php echo CHtml::dropDownList('city_id','', array(),
array('prompt'=>'Choose Your City','class'=>'form-control')); ?>
Thanks for any help.