How To Load Drop-Down List Via Ajax

Hi All

I had 2 table countries and Cities and i need to load cities via country by Ajax .

tbl_countries :

[u]id

[/u]country_name_e

tbl_cities:

     [u]id

[/u]city_name_e

country_id

View.php





....

	<div class="row">

		<?php echo $form->labelEx($model,'country'); ?>

	<?php echo chtml::activeDropDownList($model,'country',$model->getcountry(),array('prompt'=>'Select Your Countries ')

	

	

	

	); ?>

	

  

	

	

		<?php echo $form->error($model,'country'); ?>

	</div>




Model:


	

.....

	/// Get country detalis intow dropswon list 

	public function getcountry(){

    	

   	return array (

   	

   	CHtml::listData(Countries::model()->findAll(),'id','country_name_e'),

    	

   	

   	); 

    	

    	

	}

	///


////

I need small code via ajax with controller example .

Thanks in advance

http://www.yiiframework.com/wiki/24/

Thanks my bro redguy but I when i tested example on my localhost returned this message ?!


[b]Undefined index: country_id 	[/b]

view :




<?php

/* @var $this CurrentController */


$this->breadcrumbs=array(

	'Current'=>array('/current'),

	'Dynamiccities',

);

?>

<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'users-index-form',

	'enableAjaxValidation'=>true,

)); ?>

<?php

/*

 echo chtml::activeDropDownList($model,'country',$model->getcountry(),array('prompt'=>'اختر بلدك')

	

	

	

	);

	

	*/

echo CHtml::activeDropDownList('country','', array(1=>'USA',2=>'France',3=>'Japan'),

array(

'ajax' => array(

'type'=>'POST', //request type

'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.

//Style: CController::createUrl('currentController/methodToCall')

'update'=>'#city_id', //selector to update

//'data'=>'js:javascript statement' 

//leave out the data key to pass all form values through

))); 

 

//empty since it will be filled by the other dropdown

echo CHtml::dropDownList('city_id','', array());




?>




<?php $this->endWidget(); ?>


</div><!-- form -->




Controller :





class CurrentController extends Controller

{





public function accessRules()

	{

		return array(

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

            	'actions'=>array('create','update','dynamiccities'),

            	'users'=>array('@'),

        	),

		);

	}

public $country_id;

	public function actionIndex()

	{

		$this->render('index');

	}

	

public function actionDynamiccities()

{

	$data=Cities::model()->findAll('country_id=:parent_id', 

              	array(':parent_id'=>(int)  $_POST['country_id']));

 

	$data=CHtml::listData($data,'id','city_name_e');

	foreach($data as $value=>$name)

	{

    	echo CHtml::tag('option',

               	array('value'=>$value),CHtml::encode($name),true);

	}

}

	



also if change $data=Cities::model()->findAll(‘country_id=:parent_id’, in controller to

$data=Cities::model()->findAll(‘country_id=1’

return city list as text

What’s wrong here ?!!

Thanks in advance

primo:


echo CHtml::activeDropDownList('country','', array(1=>'USA',2=>'France',3=>'Japan')

is wrong. "active*" methods are only for activerecord scenarios (you must provide model instance and attribute name).

secundo:

if using active* method form element name will have name like "ModelName[attribute_name]". You could also debug and check what is really posted to actionDynamiccities.

Anyway - always better is to use Yii::app()->request->getPost( “xxxx”, “default value” ); which won’t trigger “Undefined index”.

OK , Thank you Bro . I will try to fix it .