I am newbie to yii. I am trying to create two dependent dropdownlist with one dropdownlist value. Here If I click region_id dropdownlist, it should updated cityname and cityname1 dropdownlist at the same time. I don't know how to write code for multiple ajax call. Below I have paste the code what I have tried.
<?php
echo CHtml::dropDownList('region_id','',
array(2=>'New England',1=>'Middle Atlantic',3=>'East North Central'),
array(
'prompt'=>'Select Region',
'ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('site/loadcities'),
'data'=>array('region_id'=>'js:this.value'),
'success'=>'function(html)
{
jQuery("#city_name").html(html)
}' ,
array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('site/loadcities1'),
'data'=>array('region_id'=>'js:this.value'),
'success'=>'function(html)
{
jQuery("#city_name1").html(html)
}' )
)));
echo CHtml::dropDownList('city_name','', array(), array('prompt'=>'Select City'));
echo CHtml::dropDownList('city_name1','', array(), array('prompt'=>'Select City'));
?>
Thanks in advance
You will have to write the other ajax request yourself. In fact, I prefer to store JS logic separate from view files, if it's practical (there's more than a few lines of code).
echo CHtml::dropDownList('region_id','',
array(2=>'New England',1=>'Middle Atlantic',3=>'East North Central'),
array(
'prompt'=>'Select Region',
'ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('site/loadcities'),
'data'=>array('region_id'=>'js:this.value'),
'success'=>'function(html)
{
jQuery(\'#city_name\').html(html)
}' ,
)));
echo CHtml::dropDownList('city_name','', array(), array('prompt'=>'Select City'));
echo CHtml::dropDownList('city_name1','', array(), array('prompt'=>'Select City'));
Yii::app()->clientScript->registerScript('regionChange', '
jQuery("#region_id").change(function(){
jQuery.ajax({
url: "' . Yii::app()->createUrl('site/loadcities1') . '",
type: "POST",
data: {region_id: this.value},
success: function(html) {
jQuery("#city_name1").html(html);
}
});
});
');