php - How to save data into a DB from a Modal popup in Yii2? -
i have plain view saving data correctly db, when put view modal popup page not saving data db, modal displaying. else need done in order save data db popup? thanks
this controller:
public function actioneditmypopup() { $model = new mypopupform(); if ($model->load(yii::$app->request->post()) && $model->validate()) { $model->inserteditmypopup(); return $this->renderajax('edit-mypopup', ['model' => $model]); } else { return $this->renderajax('edit-mypopup', ['model' => $model]); } }
this view of popup:
<?php pjax::begin(['enablepushstate' => false]); ?> <?php $form = activeform::begin(['id' => 'edit-mypopup-form', 'options' => ['data-pjax' => true],]); ?> <?= $form->field($model, 'attribute1') ?> <div class="form-group"> <?= html::submitbutton('submit', ['class' => 'btn btn-primary', 'name' => 'edit-mypopup-button']) ?> </div> <?php activeform::end(); ?> <?php pjax::end(); ?>
and model has variables declaration , function inserteditmypopup() insert data db.
<?php namespace app\models; use yii; use yii\base\model; class editmypopupform extends model { public $attribute1; public function rules() { return [ [['attribute1'], 'required'], ]; } public function inserteditmypopup() { //attributes name of table $a = new attributes(); $a->att1 = $this->attribute1; $a->save(); }//end function }//end class
i have overcome issue adding following code on js file:
$('body').on("click", "form#edit-mypopup-form", function() { var form = $(this); if (form.find('.has-error').length) { return false; } $.ajax({ url: form.attr('action'), type: "post", data: form.serialize(), success: function(results){ $(form).find('.results').html(results); } }); return false; });
Comments
Post a Comment