Dependent Dropdownbox

Hello friends…

     Am using Dependent dropdownlist in my form. the thing is 1.department 2.designation. 


    If we select the department field the designation want to be changed. Am using some code but i got error.





    Plz take a look at my code and help me out without error..

in my _form.php department dropdownlist


<?php  echo $form->dropDownList($model,'department',CHtml::listData(Department::model()->findAll(),'id','department'), array(

                    'prompt'=>'Select department',

                    'ajax' => array(

                        'type'=>'POST',

					    'url'=>CController::createUrl('User/designation'), 

						'update'=>'#User_designation',

                        'dataType'=>'json',

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

                        'success'=>'function(data) {

						alert("hi");

						//$("#User_designation").html(data.dropDownDesignation);

                         

                        }',

            ))); ?>

in my _form.php designation dropdownlist


<?php $desg = CHtml::listData( designation::model()->findAll(array('order' => 'designation')), 'desig_id', 'designation');

        echo $form->dropDownList($model, 'designation', $desg, array(

'prompt'=>'Select designation',       

	   // 'ajax' => array(

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

        //'url' => CController::createUrl('ajax/getCities'), //url to call.                

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

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

	    ));?>

Usercontroller.php


 public function actiondesignation()

    {

	    

          //states

           $data = Designation::model()->findAll('dept_id=:dept_id', array(':dept_id'=>(int) $_POST['department']));

			

            $data = CHtml::listData($data,'desig_id','designation');

           $dropDownDesignation= "<option value=''>Select</option>"; 

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

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

 

     

			

			

       echo CJSON::encode(array('dropDownDesignation'=>$dropDownDesignation));

		 

    //echo $dropDownStates;

	

	}

help me out and thanks…

Hi,

you can use this extenstion,

CaseCade dropDown

Oh Im sorry. So stupid of me. I mean this Correct Link

Thanks for the link @adrinna bt I didnt get what i exactly want… Now am create a new dropdown and am used ur link but i didnt get…

plz help me out i think the problem is in call the controller…

Am stuck with this for a week… plz help me out

I think the following post response could help you http://www.yiiframework.com/forum/index.php/topic/55187-combos-dependientes-estadomunicipioparroquia/page__view__findpost__p__252641 , it is in spanish, so, if you need help, let me know.

Regards

:D

@Daniel Galvan thank u for ur response.

errorI got this error help me out

Invalid argument supplied for foreach()…

this s my code on controller i got that "Invalid argument supplied for foreach()…"




public function actionDynamiccities()

 

{

  $sql = "SELECT * FROM city ".

         "WHERE state_id = :state";

  $command = Yii::app()->createCommand($sql);

  $command->bindValue(':state', $_POST['Address']['state']);

  $data = $command->execute();

  /* $data=City::model()->findAll('state_id=:state_id', 

                  array(':state_id'=>(int) $_POST ['Address']['state']));*/

 

    $data=CHtml::listData($data,'city_id','city');

 

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

    {

        echo CHtml::tag('option',

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

    }

}

I got error error in this line. if i comment this line in my _view. i didnt get error and i didnt got result…


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

Have you encountered any error? What is that error? You did’nt get what?

@andrianna I didn’t get any error! when I select that state, city want to be changed to exactly what state it belongs to. but city didnt change. now city is an empty dropdown.

Well, I think this is because of $data is empty. Are you sure $_POST has $_POST [‘Address’][‘state’]? Maybe it has another name, you can use the browser console to see exactly what parameters (POST) are you sending.

:unsure:

Try to replace ‘id’ to ‘region_id’ in actionDistricts on your controller from the link I give you…and it should work.

@adrianna and @ daniel…

Now am created the new application in yii. And am using the same code in that new one, now this dependent dropdown is working. but its not working in the current application. but i want to implement this in the current application.!!!

i don’t know y, & i didn’t face this type of uff! …

Atlast I cleared my error.

The problem is in the access rule. when we add a new function in the controller we need to add the function name in the access rule and we need to specify the authenticate for user or for the admin… that’s y i got error in POST the value to the controller.

My new function in my controller is dynamicCategory().

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

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


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


        ),

PLZ read the article carefully line by line before u start working. @amazing , @andriana , @daniel thank you for your endless support and for the yiiteam.

Yes . Use yii rights module for access control .