dependent dropdowns

Hi mans and womans,

i implemented two dependents dropdownlists,

country --> no dependent, it is loaded from all countrys of my database

state --> dependent of country

city --> dependent of state

my problem is when i change the first dropdown (country) my states change too, but my citys stay without any changes, how can i ‘force’ a submit from the state dropdownlist?

[b]a lot of thanks,

s0mk3t[/b]

[size=“1”]( sorry for my english, i’m spanish :D )[/size]

please pastebin code of the view

view code:


  <div class="row">

		<?php

		echo CHtml::label('Pais:','paises');

        echo CHtml::dropDownList('paises','', CHtml::listData(Pais::model()->findAll(),'id','nom'),

        array(

        'ajax' => array(

        'type'=>'POST',

        'url'=>CController::createUrl('Dprovincias'), 

        'update'=>'#provincias', 

        )));

        ?>

    </div>

 	<div class="row">

        <?php

		echo CHtml::label('Provincia:','provincias'); 

		echo CHtml::dropDownList('provincias','',CHtml::listData(Provincia::model()->findAll(),'id','nom'),

		array(

		'ajax' => array(

		'type' => 'POST',

		'url' => CController::createUrl('Dpoblaciones'),

		'update' => '#Persona_poblacio',

		)));

        ?>        

    </div>

    <div class="row">

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

        <?php echo $form->dropDownList($model,'poblacio',CHtml::listData(Poblacio::model()->findAll(),'id','nom'));?>

    </div>

    

    

controller code:


public function actionDProvincias()

    {

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

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

 

 

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

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

            {

                echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);

            }

 

    }

 

    public function actionDPoblaciones()

    {

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

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

 

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

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

            {

                echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);

            }

    }

You can work around your issue if in the 1st dropdown’s action instead of using


'update'=>'...'

use the


'success'=>'... js code to update both dropdowns'

and in your


actionDProvincias()

instead of returning data for the 2nd dropdown return for the 3rd as well.

Thanks man!

I have done so, following is my View’s code

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


	&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'Country_Id', CHtml::listData(Country::model()-&gt;findAll(), 'Country_Id', 'Name'), 


                        array('empty'=&gt;'Select Country',


                            'ajax'=&gt;array(


                                'type'=&gt;'POST',


                                'url'=&gt;CController::createUrl('Beneficiary/dynamicprovinces'),                                    


                                //'update'=&gt;'#'.CHtml::activeId(&#036;model, 'Province_Id') )) 


                                'dataType' =&gt; 'json',


                                'data'=&gt;array('Country_Id'=&gt;'js:this.value'),  


                                'success'=&gt;'function(data) {


                                    &#036;(&quot;#Province_Id&quot;).html(data.dropDownProvinces);


                                    &#036;(&quot;#City_District_Id&quot;).html(data.dropDownCityDistricts);


                                }',


                             )


                    )); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'Country_Id'); ?&gt;


&lt;/div&gt;





            &lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'Province_Id', array(),


                        array('empty'=&gt;'Select Province',


                            'ajax'=&gt;array(


                                'type'=&gt;'POST',


                                'url'=&gt;CController::createUrl('Beneficiary/dynamiccitydistricts'),


                                'update'=&gt;'#'.CHtml::activeId(&#036;model, 'City_District_Id'))


                    )); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'Province_Id'); ?&gt;


&lt;/div&gt;


    


    &lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;dropDownLIst(&#036;model,'City_District_Id', array(),array('empty'=&gt;'Select City/District') ); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'City_District_Id'); ?&gt;


&lt;/div&gt;

And following is controller’s code

    public function actiondynamicprovinces(){


        &#036;data=Province::model()-&gt;findAllBySql('select Province_Id, Name from mts_Province where Country_Id=' .&#036;_POST['Beneficiary']['Country_Id']);


        &#036;data=CHtml::listData(&#036;data, 'Province_Id', 'Name');


        &#036;Provinces = CHtml::tag('option', array('select'=&gt;'selected'), CHtml::encode('Select Province'), true);


        foreach(&#036;data as &#036;value=&gt;&#036;name){


            &#036;Provinces .= CHtml::tag('option', array('value'=&gt;&#036;value), CHtml::encode(&#036;name), true);


        }


        &#036;data=CityDistrict::model()-&gt;findAllBySql('select Province_Id, Name from mts_City_District where Province_Id=' .&#036;_POST['Beneficiary']['Province_Id']);


        &#036;data=CHtml::listData(&#036;data, 'City_District_Id', 'Name');


        &#036;CityDistricts = CHtml::tag('option', array('select'=&gt;'selected'), CHtml::encode('Select City or District'), true);


        foreach(&#036;data as &#036;value=&gt;&#036;name){


            &#036;CityDistricts .= CHtml::tag('option', array('value'=&gt;&#036;value), CHtml::encode(&#036;name), true);


        }


        echo CJSON::encode(array(


          'dropDownProvinces'=&gt;&#036;Provinces,


          'dropDownCityDistricts'=&gt;&#036;CityDistricts


        )); 

}

    public function actiondynamiccitydistricts(){


        &#036;data=CityDistrict::model()-&gt;findAllBySql('select Province_Id, Name from mts_City_District where Province_Id=' .&#036;_POST['Beneficiary']['Province_Id']);


        &#036;data=CHtml::listData(&#036;data, 'City_District_Id', 'Name');


        foreach(&#036;data as &#036;value=&gt;&#036;name){


            echo CHtml::tag('option', array('value'=&gt;&#036;value), CHtml::encode(&#036;name), true);


        }


    }











public function accessRules()


{


	return array(


		array('allow',  // allow all users to perform 'index' and 'view' actions


			'actions'=&gt;array('index','view'),


			'users'=&gt;array('*'),


		),


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


			'actions'=&gt;array('create','update', 'dynamicprovinces', 'dynamiccitydistricts'),


			'users'=&gt;array('@'),


		),


		array('allow', // allow admin user to perform 'admin' and 'delete' actions


			'actions'=&gt;array('admin','delete'),


			'users'=&gt;array('admin'),


		),


                    array('deny',  // deny all users


			'users'=&gt;array('*'),


		),


	);


}

But the problem is that country drop down populated, when i select a conutry, province and city_district drop downs does not populate.

Can you please help

Hi Shahzad, welcome to the forum.

These lines should be the cause of the problem.

You are using CActiveForm, so the ids of the dropdowns should be something like ‘ModelName_Provice_Id’ and ‘ModelName_City_District_Id’.

Use CHtml::activeId(), as you have already done for another dropdown: