/Hi I’ve created dependent dropdowlist with two tables with relations. I’ve this code in my view clienti (in the table there is forekey id_regione, id_comune)/
$form = ActiveForm::begin([
'method' => 'get',
]);
$items = ArrayHelper::map(Regioni::find()->all(), 'id_regione', 'nome');
echo $form->field($searchModel, 'id_regione_clienti')->dropDownList($items,
[
'prompt'=>'sceglie regione',
'onchange'=>'
$.post( "index.php?r=clienti/lists&id='.'"+$(this).val(), function( data ) {
$("select#clientisearch-id_regione_clienti").html( data);
});'
]);
$items2 = ArrayHelper::map(Comuni::find()->all(), 'id_comune', 'nome');
echo $form->field($searchModel, 'id_comune_clienti')->dropDownList($items2);
echo Html::submitButton('Find', ['class' => 'btn btn-primary']);
ActiveForm::end();
in the controller clienti I've at action for dropdownlists
public function actionLists($id)
{
$countComuni = Comuni::find()
->where(['id_regione'=>$id])
->count();
$comuni = Clienti::find()
->where(['id_regione'=>$id])
->all();
if ($countComuni >0) {
foreach ($comuni as $comune) {
echo "<option value='".$comune->id_comune."'>".$comune->nome."</option>";
}
}
}
run my application and inspect by browser chrome have this error and dropdownlist not functions
POST http://localhost/sites/basic/web/index.php?r=clienti/lists&id=1 500 (Internal Server Error)
send @ jquery.js:9175
ajax @ jquery.js:8656
jQuery.(anonymous function) @ jquery.js:8807
onchange @ index.php?r=clienti%2Fsearch:63