Answer the question
In order to leave comments, you need to log in
How to store selected value of DropDownList in database using Ajax in Yii2?
How to store selected value of DropDownList in database using Ajax in Yii2?
<?= $form->field($model, 'class')->dropDownList(\common\models\TieClass::getAll(), ['prompt' => 'Тип документа:']) ?>
<?php
$this->registerJs(<<<JS
$('#launch-class').on('change', function(){
$.ajax({
url: '/',
type: 'POST',
data : $(this).val(),
success: function(res){
console.log(res);
},
error: function(){
alert('Error!');
}
});
});
JS
);
?>
public function actionUpdate($id)
{
$model = $this->findModel($id);
if(isset($model->TieClass($model->class)->model)){
$composit = $model->TieClass($model->class)->model::findOne($id); //Проверьте пути в базе данных
$render = $model->TieClass($model->class)->form.'_form'; //Проверьте пути в базе данных
} else {
$composit = null;
$render = null;
}
if( Yii::$app->request->isAjax){
if($model->load(Yii::$app->request->post()) && $model->validate()) {
if($model->save(false)) {
return $this->redirect([$render, 'id' => $model->id]);
}
}
} elseif ($model->load(Yii::$app->request->post()) && $composit->load(Yii::$app->request->post())) {
$isValid = $model->validate();
$isValid = $composit->validate() && $isValid;
if ($isValid){
$model->save(false);
$composit->save(false);
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render('update', [
'model' => $model,
'composit' => $composit
]);
}
}
Answer the question
In order to leave comments, you need to log in
Добрый день.
В options выпадающего списка добавляете этот код:
'onchange' => '
$.post(
"path/to/controller/action",
{id : $(this).val()},
function(data){
// обрабатываете ответ сервера
}
)'
public function actionList()
{
if(Yii::$app->request->isAjax)
{
// тут Ваш код
}
return json_encode('answer'); // возвращаете ответ
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question