Hi friends,
I am completely new to yii… Currently am working on a project and in that i want dependent dropdowns… I have 2 tables tbl_country, tbl_states…
_form.php
<div class="row">
<?php echo CHtml::dropDownList('country','',CHtml::listData(Country::model()->findAll(array('order'=>'countryname ASC')),'country_id','countryname'),array('prompt'=>'Select', 'id'=>'countryid',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('dynamicstates'),
'update'=>'#stateid',
'data'=>'js:$(this).serialize()',
))); ?>
</div>
<?php echo CHtml::dropDownList('state','',array(),array('prompt'=>'Select','id'=>'stateid')); ?>
controller.php
public function actionDynamicstates()
{
$data=Country::model()->findAll('country_id=:countryid',
array(':countryid'=>(int) $_POST['countryid']));
echo CHtml::tag('option', array('value' => 0), CHtml::encode('Select'), true); // for select
$data=CHtml::listData($data,'state_id','statename');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
Please help me to find the solution…
Thanks…