Depedant drop downlist

Hi guys,

I’m trying to use an depedant dropdownlist using the yii approach and following the tutorial for this purpose, but the “onchange” attribute is never created!!. I’m using the 1.1.4 yii version

This is my code in the view:


<?php 

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

array(

'ajax' => array(

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

'url'=>CController::createUrl('country/Dynamiccities'), //url to call.

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

))); 

 

echo CHtml::dropDownList('city_id','', CHtml::listData(Cities::model()->findAll(),'cities_id','name'));

?>

In my country controller:


public function actionDynamiccities()

	{

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

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

	 

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

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

	    {

	        echo CHtml::tag('option',

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

	    }

	}

But the code generate in my view is :




<select id="country_id" name="country_id">

<option value="1">USA</option>

<option value="2">France</option>

<option value="3">Japan</option>

</select>

But i should have the "onchange" attribute with something like : onchange="country/dynamiccities" !

Do you have an idea why i don’t have the good code in my view ?

Thanks

You are better off using this extension:

http://www.yiiframework.com/extension/jquerycascadeselect

or

http://www.yiiframework.com/extension/jquery-cascade

So you dont need to worry about all the logic in this.

Thanks sir for this quick response, i’ll read those tutorials now :)

Now the ajax call work and im able to call my action


dynamiccities

, but i have an other error now which is: Undefined index: country_id.

Do you have an idea why i have this error ? because in the dopdwonlist i have the "country_id" index !

In my action i added


var_dump($_POST)

, and it’s empty because it return


array(0) {}

Thanks

Check firebug, Im not familiar with the extensions, they might be using GET to send the parameters, Undefined index: country_id. is clearly a problem in your controller’s action