<?php
// part of the country dropdownlist definition
...
'success'=>'js:'.
'function(html){$("#city_id").html(html); '.
'$("#selected_country").attr("innerHTML", $("#country_id :selected").val()); '.
'$("#city_id option:first").before("<option value='empty'>--Select City--</option>"); '.
'$("#city_id option:first").attr("selected", "selected"); '.
'$("#city_id").removeAttr("disabled"); '.
'}',
...
// optional presentation
echo 'You selected: <span id="selected_country">nothing yet</span>';
?>
<?php
// part of the city dropdownlist definition
...
'empty'=>'--Select City--',
'disabled'=>'true',
'ajax' => array(
...
?>
Feel free to optimize, test with different browsers, consider what would happen if the server becomes temporarily unavailable. I haven't tested the latter.
I got it to work! But need help where the code shud be appropriately placed.
in my view/site/index.php, I have
<?php
// Shud be in moved WHERE?
$criteria=new CDbCriteria;
$criteria->select = "ansi_code, state_name";
$criteria->order = "state_name asc";
$states=State::model()->findAll($criteria);
?>
<?php echo CHtml::form('','get'); ?>
State:
<?php echo CHtml::dropDownList('ansi_code',
isset($_GET['ansi_code'])?(int)$_GET['ansi_code']:0,
CHtml::listData($states,'ansi_code','state_name'),
//array('empty'=>'All categories', 'submit'=>''));
array(
'empty' => 'Select State'
,'ajax' => array(
'type'=>'POST' //request type
,'url'=>'index.php?r=city/citiesinstate' //url to call
,'update'=>'#ansi_code' //selector to update
,'success'=>'js:function(html){jQuery("#feature_id").html(html);}', //selector to fill
)));
// State has many Cities - empty since it will be filled by the other dropdown
echo CHtml::dropDownList('feature_id','',array('empty'=>'Select State First',));
?>
in controllers/CityController.php
<?php
public function actionCitiesinstate()
{ //print_r($_POST);
$criteria=new CDbCriteria;
$criteria->select = "feature_id, feature_name";
$criteria->order = "feature_name asc";
$criteria->condition='ansi_code='.$_POST['ansi_code'];
$data=City::model()->findAll($criteria);
//$data=City::model()->findAll('ansi_code=:ansi_code');
$data=CHtml::listData($data,'feature_id','feature_name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
die();
}
It works but the code placements are not right. Learning.