Dependent dropdowns

Hi,

I’m only new to ajax and yii. I’ve been having some problems with dependent drop downs. I’ve followed the the cookbook (cookbook 24) with no luck : (

So here is what I’ve done create a Test table with a country and city column in it. Generated the model/crud using yiic.

Then i edit the _form.php as follows




        <div class="row">

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

                <?php echo $form->dropDownList($model,'Country',array(1=>'USA',2=>'Australia',3=>'Thailand'), array('ajax'=>array('type'=>'POST','success'=>'alert("Hi")', 'url'=>'test/dynamiccities','update'=>'#City'))); ?>

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

        </div>

        <div class="row">

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

                <?php echo $form->dropDownList($model,'City',array()); ?>

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

        </div>



I added the success alert to see if its running it seems to but…

Added the following code code to the TestController.php




       public function actionDynamiccities() {

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

       }



And added the function to the security




public function accessRules()

        {

                return array(

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

                                'actions'=>array('index','view','Dynamiccities'),

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

               



I get no update on the second drop down.

Still can working out whats wrong. Any help would be greatly appreciated

Thanks in advance

Iain

Edit:

Fixed typo in controller name

In the call _form.php you have ‘test/dynamiccities’ but later you say that the controller is TextController.php

so if the controller is TextController you should call ‘textdynamiccities’

not sure if it matters, but in the accessRules put Dynamiccities all lowercase as is index and view

Sorry that’s a typo in this post the controller is TestController.php. So i believe that means the controller should be test/dynamiccities, i’ve used nearly all possible cases to try and get this to work without success

check the cookbook again… the url uses CControler::createUrl()… that part is missing in your example…

I’ve been trying to get dependent dropdowns to work for 8 hours now so far today and tonight.

Could somebody please simply post the full php code (controller, view and model) that demonstrates a working ajax dependent dropdown (or listbox, etc.)?

It could be a CRUD screen or a form. Anything that actually works would be great. I’ve been playing around with the cookbook example for 8 hours now and I’m not getting anywhere. I am using netbeans and I’ve put a break in actionDynamiccities() and have yet to figure out how to get that part of the code to even execute, much less work.

As I have written before…

in the ajax call you have:


'url'=>'test/dynamiccities'

check again the cookbook

there is


'url'=>CController::createUrl('currentController/dynamiccities')

so you should try it like this


'url'=>CController::createUrl('test/dynamiccities')

Try to change this


...'update'=>'#City'))); ?>



to something like


...'update'=>'#nameOfYourModel_City'))); ?>



Awesome thanks mdomba and abajja both fixes where needed.

For stickdog here are the two pieces of code

_form.php


 

     ....

        <div class="row">

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

                <?php echo $form->dropDownList($model,'Country',array(1=>'USA',2=>'Australia',3=>'Thailand'), array('ajax'=>array('type'=>'POST', 'url'=>CController::createUrl('Test/dynamiccities'),'update'=>'#test_City'))); ?>

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

        </div>


        <div class="row">

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

                <?php echo $form->dropDownList($model,'City',array()); ?>

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

        </div>

     ....



where #test_City is the the name of the model and dependent drop down, check your model dir for case etc as it seems to be case sensitive, the url one is not case sensitive

TestController.php




     ....

        public function actionDynamiccities() {

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

        }

        /**

         * Specifies the access control rules.

         * This method is used by the 'accessControl' filter.

         * @return array access control rules

         */

        public function accessRules()

        {

                return array(

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

                                'actions'=>array('index','view','dynamiccities'),

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

                        ),

     ....



Add the dependent code in the actionDynamiccities function and add ‘dynamiccities’ access rules.

And bobs your uncle it should work. Hope that helps…

Ok so after getting that to work my next problem is getting the post data.

so i changed the actionDynamiccities to




public function actionDynamiccities() {

    echo CHtml::tag('option', array('value'=>$_POST['Country']), $_POST['Country'], true);

}



to check that i could get the country id across.

but i get this in the html




<option value=""></option>



Which indicates to me that nothing is coming across.

When is use firebug and look at the post info you can see the value




Parameters      application/x-www-form-urlencoded

test[Country]	2


Source

test%5BCountry%5D=2



So i think I should be trying to access ‘test[Country]’ instead of ‘County’ but can’t work out a way to do it. Any Suggestions?

Thanks

Always the way figured it out 5 minutes later.

Just for other the following worked




 public function actionDynamiccities() {

                echo CHtml::tag('option', array('value'=>$_POST['test']['Country']), $_POST['test']['Country'], true);

        }



OK, I got this to work finally. I can make the first form element (a drop down) user selection fill a dependent list box that becomes the second form element. THANK YOU ALL!!!!

But now I have a new huge problem. If any of form validation does not work, the listbox (second form element) is cleared of all of its display data as well as all of the user choices (if any), but the first form element (a drop down) selection that the listbox values are dependent on is retained. How can I change this? I’d like to retain the listbox data, but I’d settle for reseting the drop down to the prompt if validation fails.

Thanks again.

Hello,

i have problem with drop down list if i have second dependent dropdownlist.

example i have subcity




country : Indonesia

City    : Jakarta

SubCity : Jakarta South, Jakarta Centre < Jakarta North



If I change Country from ‘Indonesia’ to ‘Singapore’, Subcity wont change it value…

i must change value in city first…

Any Solution?

Just my two cent on this issue, so other people don’t run into it:

It is important to understand that the "currentController" is NOT, repeat: NOT the Controller that you usally find in the "controllers" folder.

I was running into that trap trying something like "PostController/Dynamicposts", while I should have written "Post/Dynamicposts".

Hope that´ll save some time for some people :D

Cheers,

Frank

hi the dependent drop is working well …

but,

i have another problem when editing the country is automatically loaded but cities are not getting loaded

can anyone please help me out