Difference between #3 and #4 of An Easy Solution for Dependent dropDownList Using AJAX

unchanged
Title
An Easy Solution for Dependent dropDownList Using AJAX
unchanged
Category
Tips
unchanged
Tags
AJAX, drop down list
changed
Content
Sometimes new Yii guys face problem to manage dependent dropDownList using AJAX.
I am going to discuss about an easy solution about this issue.

Example code:

Code in View-


~~~
[php]
<?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'=>CController::createUrl('loadcities'),
    'update'=>'#city_name', 
  'data'=>array('region_id'=>'js:this.value'),
  ))); 
									 
								
 
echo CHtml::dropDownList('city_name','', array(), array('prompt'=>'Select
City'));
?>
~~~

Code in Controller-



~~~
[php]
public function actionLoadcities()
{
   $data=RegionCity::model()->findAll('region_id=:region_id', 
   array(':region_id'=>(int) $_POST['region_id']));
	 
   $data=CHtml::listData($data,'id','city_name');
		
   echo "<option value=''>Select City</option>";
   foreach($data as $value=>$city_name)
   echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($city_name),true);
}
~~~

I think this will help to understand actually how AJAX works in Yii for
dependent dropDownList also AJAX working behavior in Yii framework.

--Enjoy, Explore... Yii
 
Write new article
  • Written by: mrs
  • Updated by: KonApaz
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +11 / -1
  • Viewed: 39,627 times
  • Created on: Dec 9, 2012
  • Last updated: Apr 15, 2014
  • Tags: AJAX, drop down list