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