hers the solution
in my _Form
<?php if(isset($_POST['your_Model']['country']))
{$countryid = $_POST['your_Model']['country'];}
else {$countryid = ''; }?>
<?php echo $form->dropDownList($model,'country',
CHtml::listData(Country::model()->findAll(), 'country_id', 'country'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('your_Model/dynamicStates'), //url to call.
'update'=>'#'.CHtml::activeId($model,'state'),
array('class'=>'ajaxlink'),
),'prompt'=>'--select--',)
); ?>
</td>
<td>
<?php echo $form->labelEx($model,'state'); ?>
</td>
<td>
<?php $listStates = array();
if(!empty($countryid))
{
$listStates = $this->getStatesList($countryid);
}
if ($model->state != '')
{
$listStates = $this->getStatesList($model->country);
}
?>
<?php echo $form->dropDownList($model,'state',$listStates, array('empty'=>'--select--')); ?>
and in controller or model
public function actionDynamicStates(){
$data = $this->getStatesList($_POST['your_Model']['country']);
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
public function getStatesList($country_id) {
$data=State::model()->findAll('country_id=:id',
array(':id'=>(int)$country_id));
return CHtml::listData($data,'state_id','state');
}
Hope this Helps You If You want I Can Explain You