Yii Framework Forum: Dependent Dropdown 404 (Not Found) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Dependent Dropdown 404 (Not Found)

#1 User is offline   noobkoder 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 02-November 17

Posted 07 March 2018 - 10:42 AM

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

0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,648
  • Joined: 16-February 11
  • Location:Japan

Posted 07 March 2018 - 12:09 PM

Hi,

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

#3 User is offline   noobkoder 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 02-November 17

Posted 07 March 2018 - 12:55 PM

 softark, on 07 March 2018 - 12:09 PM, said:

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?
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,648
  • Joined: 16-February 11
  • Location:Japan

Posted 07 March 2018 - 05:56 PM

In the wiki artile(http://www.yiiframew...cratch-in-yii2/), the 2nd dropdown is filled with all the items at the beginning:

Quote

    $dataPost=ArrayHelper::map(\common\models\Post::find()->asArray()->all(), 'id', 'title');
    echo $form->field($model, 'title')
        ->dropDownList(
            $dataPost,           
            ['id'=>'title']
        );



You can set "$dataPost" empty.
0

#5 User is offline   noobkoder 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 02-November 17

Posted 08 March 2018 - 06:25 AM

 softark, on 07 March 2018 - 05:56 PM, said:

In the wiki artile(http://www.yiiframew...cratch-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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users