Dependent Dropdown 404 (Not Found)

Hello, I am creating a dependent dropdown for Region and City at the signup page. It is really confusing, I have been trying to figure out what’s wrong and I cannot really find where did I get wrong.

The code on signup form


<?= $form->field($model, 'region_id')->dropDownList(ArrayHelper::map(Region::find()->all(), 'id', 'Region_Name'),

                                            [

                                                'prompt' => 'Select Region',

                                                'style' => 'width:250px',

                                                'onChange' => '

                                                    $.post("index.php?r=region/lists&id='.'"+$(this).val(), function( data) {

                                                        $( "select#signup-city-municipal_id" ).html( data );


                                                    });'

                                            ]


                ) ?>

Here’s the method code on City Controller


 public function actionLists($id)

    {

        $CityMunicipal = CityMunicipal::find()

                        ->where(['region_id' => $id])

                        ->all();


        if (!empty($CityMunicipal)) {

            foreach ($CityMunicipal as $cm) {

                echo '<option value="' . $cm->id . '">' . $cm->CityMunicipal . '</option>';

            }

        }


        else {

            echo "<option>Select Municipal</option>";

        }

    }



Hi,

City controller? But you are calling lists action of region controller, aren’t you?

Hello thanks for reply

you mean I should put the method actionLists on the Region controller?

This is where I referred to


http://www.yiiframework.com/wiki/723/creating-a-dependent-dropdown-from-scratch-in-yii2/

I also noticed that I was wrong at the onChange part so I changed city-municipal_id to city_municipal_id and now I’m getting error 500 (Internal Server Error)

EDIT:

I understand what you said now I got wrong at the


"index.php?r=region/lists&id='.'

part. I’ve fixed it now to


"index.php?r=city-municipal/lists&id='.'

it doesn’t show anymore errors at the console but it still does not work.

Edit no.3

I’ve fixed it now

It should be


select#signupform-city_municipal_id

instead of


elect#signup-city_municipal_id

I’ve checked the elements and I found out that I was wrong so I changed it, by the way how do I make the list empty on City Municipal Dropdown if I have not select a region yet?

In the wiki artile(http://www.yiiframework.com/wiki/723/creating-a-dependent-dropdown-from-scratch-in-yii2/), the 2nd dropdown is filled with all the items at the beginning:

You can set "$dataPost" empty.

Thank you, I got it now.