Chained Select Box Content from Database

Hi good day…

In my user creation I have

Country:

[Select Box Here]

State/Province:

[Select Box Here]

City/Town:

[Select Box Here]

Here are the sample table designs

tbl_countrymaster

id country

1 Philippines

2 Japan

tbl_provincemaster

id countryid province

1 1 Isabela

2 1 Neuva Ecija

3 2 Izumi

4 2 Yamato

tbl_citymaster

id provinceid city

1 1 Santiago

2 1 Aurora

3 2 Gapan

4 3 Naruto

5 4 Madara

What I want to happen is a chained select box from this tables…

Following may help:

Creating a dependent dropdown

jQuery Chained

thanks for the link… I tried the link Creating a dependent dropdown but it does not work… I used the example code and change the controllername but it does not work. I already used jquery chained in my past projects but I don’t know how to implement it in Yii…

Dependent dropdown is pretty easy, may be you should look at it in simpler way like this.

  1. Render parent drop down.

  2. Render child drop down.

  3. On change of parent drop down, make an ajax call to server with value of parent drop down and get the option-list in response to be populated in child drop down.

  4. Based on response, remove current elements from child drop down and update new elements from response.

Hope you could manage above independent steps. If you could, then the code sample given in Dependent Dropdown example is combination of above steps into just one.

Hi mastermunj, with the example given in dependent drop down I wasn’t able to make it work. I just wanted to make it work first, could help me in my code, here it is:

In view:

echo CHtml::dropDownList(‘country_id’,’’, array(1=>‘USA’,2=>‘France’,3=>‘Japan’),

array(

‘ajax’ => array(

‘type’=>‘POST’, //request type

‘url’=>CController::createUrl(‘useraccounts/dynamiccities’), //url to call.

//Style: CController::createUrl(‘currentController/methodToCall’)

‘update’=>’#city_id’, //selector to update

//‘data’=>‘js:javascript statement’

//leave out the data key to pass all form values through

)));

//empty since it will be filled by the other dropdown

echo CHtml::dropDownList(‘city_id’,’’, array());

In controller

array(‘allow’, // allow authenticated user to perform ‘create’ and ‘update’ actions

            'actions'=>array('create','update','dynamiccities'),


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


        ),

public function actionDynamiccities()

{

//please enter current controller name because yii send multi dim array

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


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





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


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


{


    echo CHtml::tag('option',


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


}

}

The problem seems with the way you’ve used attributes in view and in action.

  1. In view you’re directly using ‘country_id’ & ‘city_id’, whereas in action you are trying to read $_POST[‘Useraccounts’][‘country_id’].

Check out proper usage for CActiveForm dropdownlist and then you will have problem sorted out in no time.