[Solved]problem with dependent dropdownlist

I did Matt, when I change the Region, Lab still blank.




<?php 

	        

            $list=CHtml::listData(Region::model()->findAll(),'id','region'); 

            echo CHtml::activeDropDownList($model, 'Region_id', $list, array(

            

           'empty'=>'Please Select Region', 

           'ajax' => array( 

           'type'=>'POST', //request type 

           'url'=>CController::createUrl('test/dynamicRegion'), //url to call. 

           'update'=>'#Lab_id', //selector to update 

           'data'=>'js:$(this).serialize()', 

            

           )));

           

           ?>

           <?php 

            	

              echo CHtml::activeDropDownList($model,'Lab_id', array(),array('id'=>'Lab_id'));

                               

            ?>




Can you verify that your controller is receiving the POST variable? Try to echo it out - see this post for an example.

Also verify, by viewing page source, that the DOM element Id’s are correct?

Matt

Without CHtml::activeDropDownList on Region, it’s working fine, but when I use activeDropDownList, Lab field not changing. Here is my controller. I appreciate if you change my code and let me know where I need to change




public function actionDynamicRegion(){

                $data=Lab::model()->findAllBySql('select * from Lab where Region_id='.$_POST['Region_id']); 

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

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

                { 

                        echo CHtml::tag('option', 

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

                } 

		


    } 



This is working for me.

Notice

  • I’m not setting the Id of the 2nd drop down list. I.e. NO echo CHtml::activeDropDownList($model,‘Lab_id’, array(),array(‘id’=>‘Lab_id’));

  • I am setting the key name of the post variable - categoryId

  • I’m using js:this.value to explicitly set the value.

  • The previous two points are just my preference; your method should work too.

Let me know how it goes.




        <div class="row">

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

            <?php echo CHtml::activeDropDownList($model, 'category_id', 

                CHtml::listData(Category::model()->findAll(), 'id', 'name'), array(

                'empty' => 'Please Select Region',

                'ajax' => array(

                    'type' => 'POST',

                    'url' => CController::createUrl('/project/dynamicSubCategory'), //url to call. 

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

                    'data' => array('categoryId' => 'js:this.value'),

                )));

            ?>

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

        </div>


        <div class="row">

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

            <?php echo CHtml::activeDropDownList($model, 'sub_category_id', array()); ?>

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

        </div>






public function actionDynamicSubCategory()

    {

        $data = SubCategory::model()->findAll('category_id=:category_id', array(':category_id' => $_POST['categoryId']));

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

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

        {

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

        }

    }



Cheers,

Matt

hi,

I used your example it is working fine, but the the employee field is not updated in the database. The department field is updated but not the employee field.

Thanks,

Thanks so much! I can’t do it this, but your topic helps me a lot!