<?= $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
]);
}
}
'onchange' => '
$.post(
"path/to/controller/action",
{id : $(this).val()},
function(data){
// обрабатываете ответ сервера
}
)'
public function actionList()
{
if(Yii::$app->request->isAjax)
{
// тут Ваш код
}
return json_encode('answer'); // возвращаете ответ
}
jquery.js?v=1506282721:9175 POST http://backend.yii2-core.dev/noty/default/index 404 (Not Found)
public function actionUpdate($id)
{
$model = $this->findModel($id);
if( Yii::$app->request->isAjax){
$isValid = $model->validate();
if($isValid){
$model->class = key($_POST);
$model->save(false);
}
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
public function actionUpdate($id)
{
if( Yii::$app->request->isAjax){
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->class = key($_POST); // что это?!! зачем?!!
if($model->save(false)){
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
}
Да суть в том что контроллер с экшеном у меня по другому называются, видимо в сборке что то не так.
Array
(
[2] =>
)
public function actionAjax($id)
{
if( Yii::$app->request->isAjax){
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if($model->save(false)){
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
}
// вначале файла
use common\models\TieClass;
$form->field($model, 'class')->dropDownList(TieClass::getAll(), ['prompt' => 'Тип документа:',
'onchange' => '
$.post(
"path/to/controller/action",
{id : $(this).val()},
function(data){
// обрабатываете ответ сервера
}
)'
])
<?php
$id = $model->id;
$this->registerJs(<<<JS
$('#launch-class').on('change', function(){
$.ajax({
url: '/launch/update?id={$id}',
type: 'POST',
data : $(this).val(),
success: function(res){
console.log(res);
},
error: function(){
alert('Error!');
}
});
});
JS
);
?>
это ведь не поможет исправить проблему записи?
Таким образом запрос отправляется на тот же экшн откуда он оправляется.
'/launch/update?id={$id}'
Я вам написал пример и сказал что именно как вы написали вылетает 404
"path/to/controller/action"
обозначает "путь/к/контролеру/действию"
!!!