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!