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

1)products–>product_id,product_name,cat_id,subcat_id

2)products_category–>cat_id,cat_name

3)products_subcategory–>subcat_id,subcat_name

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, 

                            array( 

                                'ajax' => array( 

                                'type'=>'POST', 

                                'url'=>CController::createUrl('ProductsSubcategory/dynamicSubcategory'), 

                                'update'=>'#subcat_id'))); 

 

ay; 


		 ?>

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

	</div>

         <div class="row">

    

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

                

             <?php

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

			  ?>


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

        </div>

and here’s the code in the controller:




 public function actionDynamicsubcategory()  

  {

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

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

	

	$data=CHtml::listData($data,'subcat_id','subcat_name');

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

	{

		echo CHtml::tag('option',

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

	}

 }




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

Here is a nice wiki article about that - http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown

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 working.do i need to do anything else to make ajax work?

thanks…

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 - http://www.yiiframework.com/forum/index.php?/topic/11531-solved-how-to-create-dependent-dropdown-lists/page__view__findpost__p__57080

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>

</select> 

                 

        </div> 

         <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">

</select> 

 

                

        </div>

 



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…

‘update’=>’#’.CHtml::activeId($model,‘subcat_id’),

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']; 

            $data=ProductsSubcategory::model()->findAll('cat_id=:cat_id', 

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

 

            $data=CHtml::listData($data,'subcat_id','subcat_name'); 

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

                echo CHtml::tag('option', 

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

            } 

 

          

        }

		



I was going wrong in the controller…then tried this…$cat_id = $_POST[‘Products’][‘cat_id’]; … to get the chosen category id.it 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?