dependent dropdown

hi!! i’m usin yii framework for the first time and even after goin throu so many forums i stil cant figure out how to populate dependent dropdown. i thoroughly went throu the country and city example but cant get it working.

somebody please help me out… thanks,

There are many posts about that here…

If they do not help you… best way to get help would be to explain better what exactly do not work… and to post your code… of course…

I have a product list where i need to enter new products. each product has a category and a subcategory.

everytime a new product is to be entered in the database, the category and the sub category to which it belongs is to be selected using dropdowns. i am able to populate the category dropdown but not the subcategory dropdown as it has to populate based on the category selected.

tables in the database




here i present the code in _form.php

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

		<?php$productCategoriesArray = CHtml::listData(ProductsCategory::model()->findAll(),'cat_id','cat_name'); 

                   echo $form->DropDownList($model,'cat_id',$productCategoriesArray, 


                                'ajax' => array( 







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


         <div class="row">


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



			echo $form->dropDownList($model,'subcat_id',array()); 


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


and here’s the code in the controller:

 public function actionDynamicsubcategory()  



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



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


		echo CHtml::tag('option',




As I wrote above there are many post regarding dependent dropdowns… did you read them?

Here is a nice wiki article about that -

On the first dropdown you set to update "#subcat_id"… but that is not the ID of the second dropdown

And on the first dropdown URL you are using "dynamicSubcategory" (big S), but the method is "actionDynamicsubcategory" (no big S)

ok i changed s to S. still not working.and subcat_id is the id of the second dropdown.

and also anything i try to do with ajax call is not i need to do anything else to make ajax work?


You’re sure about that… have you checked the HTML source to see the generated ID ?

And again… Have you searched the forum for similar posts?

Check this post -

yes i did… actually i have been referrin that post itself. and trying to do something similar. only difference is i want it in ma Products model and there it is in susubcategory model. i did the required changes. still not working. really sorry for the trouble… this actually seems to be a very easy thing…:(

<div class="row"> 

	<label for="Products_cat_id" class="required">Category <span class="required">*</span></label> 

                <select name="Products[cat_id]" id="Products_cat_id">

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

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

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

<option value="4">electronic</option>




         <div class="row"> 


                <label for="Products_subcat_id" class="required">Sub Category <span class="required">*</span></label>  


             <select name="Products[subcat_id]" id="Products_subcat_id">






This is the result of view source for create page

As you see the ID of the second dropdown is not “subcat_id” but “Product_subcat_id”… so it’s better to use activeId() for reffering the autogenerated ID’s…

post your "changed" code now…


i used this for update

i got it working:) :) thank you so much. i’ll come back for anymore issues…

For future reference… can you write what have you done to make it working…

public function actionDynamicSubcategory()   


				   $cat_id = $_POST['Products']['cat_id']; 


                    array(':cat_id'=> $cat_id)); 



            foreach($data as $value=>$subcategory)  { 

                echo CHtml::tag('option', 







I was going wrong in the controller…then tried this…$cat_id = $_POST[‘Products’][‘cat_id’]; … to get the chosen category worked…

similarly, i want to update a textfield once one dropdwn is selected and append something else to the same textfield once 2nd dropdown is selected.

Basically i want to generate a code.

need some help…

Any particular post that i can refer to?