Dependent Dropdown 500 (Internal Server Error)

Hello everyone! This is the first time I have done a dependent dropdown, my issue is that I’m getting an error in ‘Console’ that was provided in inspect elements in internet browsers.

My error is:

Also in my form when I select a category a list of sub categories will appear on my dropdown. For example in category I selected Housekeeping Services, supposed to be ‘Extra Bottle Water’ ‘Room Cleaning’ ‘Extra Pillow and Blankets’ will appear on subcategory dropdown. But when I choose a category nothing happens, the sub category just list down all sub categories.

Here’s what I have done in code.

SubCatController:


 public function actionLists($id)

    {

        $countSubCat = SubCat::find()

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

                    ->count();


        $SubCat = SubCat::find()

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

                    ->count();


        if ($countSubCat > 0) {

            foreach($SubCat as $SubCat) {

                echo "<option value'".$SubCat->id."'>".$SubCat->sub_category."</option>";

            }

        }

        else {

            echo "<option>-</option>";

        }

    }

In Tickets form




 <?= $form->field($model, 'category_id')->dropDownlist(

                                                ArrayHelper::map(Category::find()->all(), 'id', 'category_name'),

                                                [

                                                    'prompt' => 'Select Category',

                                                    'onchange' => '

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

                                                            $( "select#models-contact" ).html( data );

                                                        });'


                                                ]

    ); ?>


     <?= $form->field($model, 'sub_cat_id')->dropDownlist(

                                                ArrayHelper::map(SubCat::find()->all(), 'id', 'sub_category'),

                                                [

                                                    'prompt' => 'Select Sub Category',

                                                    

                                                ]

    ); ?>




I can’t figure out what I have done wrong, I’m so confused. Can anyone help me out? Thanks in advance!

Is this exact copy of your code?

Note that both $countSubCat and $SubCat are a count()… so later you are doing a foreach() on a number.

Also "foreach($SubCat as $SubCat)" seems wrong as you use the same variable name.

Yes, this is the exact copy of my code, I’ve follow the tutorial on yii wiki.

I think this is where I got wrong. I currently checking my code, I hope I can fix it.


foreach($SubCat as $SubCat)

EDIT: Can anyone help me I’m really stack here

This is where I got wrong


foreach($SubCat as $SubCat)

So basically they are supposed to be not the same variable

the first $SubCat is the one I got from:

[code]$SubCat = SubCat::find()

                -&gt;where(['category_id' =&gt; &#036;id])


                -&gt;count();[code]

How about the second variable where do I get it? I think this is the solution but I have no idea where I’m going to get the second variable.

not only the variable names conflict but there are other errors as well. you are doing 2 queries just to render an option list you can get away with just 1 query and still get the same result. whoever posted that tutorial on wiki should fix it.

here is fixed version of your code




$subCategories = SubCat::find()

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

    ->all();


if (!empty($subCategories)) {

    foreach($subCategories as $c) {

        echo '<option value="' . $c->id . '">' . $c->sub_category . '</option>';

    }

} else {

    echo "<option>-</option>";

}

can you provide a link to the wiki tutorial.

Thanks! I’ll try this one thank you for the help, if it doesn’t work I think there is something wrong with the relationship in database sorry I’m not really good at this. Btw this is the link that I’ve read http://www.yiiframework.com/wiki/723/creating-a-dependent-dropdown-from-scratch-in-yii2/