Ajax Update Of Dependantdropdown In Yii

I have 3 dependent drop-downs, data is populated in all of them. But when i select 1st dropdown, (its dependant dropdown) 2nd dropdown populates but it does not populate the 3rd dropdown. I have to click the value in 2nd dropdown for population of values in 3rd dropdown.

I want that 3rd dropdown to be automatically populated on population of 2nd dropdown.Is it possible?


<div class="row">

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

        <?php echo $form->dropDownList($model, 'province_id', $model->getProvinces(), array(

            'prompt' => '–--Select Provinces--–',

            'ajax' => array('type' => 'POST',

                'url' => CController::createUrl('candidate/district'),

                'update' => '#'.CHtml::activeId($model,'district_id'),

                'data' => array(

                    'province_id' => 'js:this.value',

                ),

            )

        ));

        ?>

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

    </div>




    <div class="row">

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

        <?php

        $dist1 = CHtml::listData(District::model()->findAll('id= :district_id',

            array(':district_id'=>$model->district_id)), 'id', 'name');

        echo $form->dropDownList($model, 'district_id', $dist1, array(

            'prompt' => '–--Select District--–',

            'ajax' => array('type' => 'POST',

                'url' => CController::createUrl('candidate/county'),

                'update' => '#'.CHtml::activeId($model,'county_id'),

                'data' => array(

                    'district_id' => 'js:this.value',


                ),

            )

        ));

        ?>

    </div>


<div class="row wide">

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

        <?php echo $form->dropDownList($model,'county_id', CHtml::listData(County::model()->findAll('id= :county_id',

                array(':county_id'=>$model->county_id)), 'id', 'name')

        ); ?>

    </div>

I’m not sure but I think that upon populating the second dropdown you would have to select a default value from which to populate the 3rd dropdown. Your code indicates the common “country-state-city” scenario and from what you’re saying it’s working as it’s supposed to. Check your logic; do you really want to select a county before selecting a district?

Hi

i am not sure this suitable or not follow this method…

call js function on dropdownlist onchange

[color="#8B0000"]Use in view php page

<td><?php echo $form->labelEx($model,‘country’);?></td>

&lt;td&gt;&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'country',CHtml::listData(&#036;country,'Country_Code','Country_Name'),array('prompt'=&gt;'-- Select--','onchange'=&gt;'ajaxState(this.value)')); ?&gt;&lt;/td&gt;








 &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'state');?&gt;&lt;/td&gt;


 &lt;td&gt;&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'state',array(''=&gt;'--Select--'),array('id'=&gt;'state','onchange'=&gt;'ajaxDistrict(this.value)'));?&gt;&lt;/td&gt;

Use Javascript function in view//

function ajaxState(obj,obj1)


{	





	if(obj=='') return false;





	var url='&lt;?php echo Yii::app()-&gt;baseUrl;?&gt;/index.php/Ajax/Ajaxsate?countryCode='+obj+'&amp;statecode='+obj1;


	&#036;(&quot;#state&quot;).load(url);		


	


}


function ajaxDistrict(obj,obj1)


{


if(obj=='') return false;


if(obj==9999) &#036;('#otherState').show();


else {


	&#036;('#otherState').hide();


	&#036;('#OfficialForm_oState').val('');


	var url='&lt;?php echo Yii::app()-&gt;baseUrl;?&gt;/index.php/Ajax/Ajaxdistrict?stateCode='+obj+'&amp;DistrictCode='+obj1;


	


	&#036;(&quot;#district&quot;).load(url);


	}


	


	


}

use code in contoller

public function actionAjaxsate()

{


	


	&#036;query = &#036;this-&gt;createCommand('sp_Getstateview');


	mssql_bind(&#036;query,'@Country_Code',&#036;countrycode,SQLINT4,false,false,5);


		&#036;result =mssql_execute(&#036;query);	





	&#036;lstArray=array();


	


	while(&#036;field=mssql_fetch_object(&#036;result ))


	{	


		&#036;lstArray[] = &#036;field;


	


	} 


	&#036;ajaxstate = &#036;lstArray;


	


	&#036;data=CHtml::listData(&#036;ajaxstate,'State_Code','State_Name');


	echo '&lt;option value=&quot;&quot;&gt;Select...&lt;/option&gt;';


	foreach(&#036;data as &#036;State_Code=&gt;&#036;State_Name)


	{


		if(&#036;State_Code == &#036;_GET['statecode'])


			echo CHtml::tag('option',


               array('value'=&gt;&#036;State_Code,'selected' =&gt; 'selected'),CHtml::encode(&#036;State_Name),true);


		else


    		echo CHtml::tag('option',


               array('value'=&gt;&#036;State_Code),CHtml::encode(&#036;State_Name),true);


	} 


}	

[/color]