S
S
Sergey2015-04-25 21:56:08
Yii
Sergey, 2015-04-25 21:56:08

How to do Ajax Yii2 autosave?

Data is not being added to the database, what should I do?
the form

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<div class="activation-form">

    
    <?php $form = ActiveForm::begin([
        'id'=>'formid'
    ]); ?>
    <?= Html::activeTextInput($model, 'ik_co_id', ['class'=>'form-item req', 'value' => "{$_POST['ik_co_id']}"]) ?>
    <?php ActiveForm::end(); ?>

<script>
//    $('#formid').on('submit', function(e) {
//    e.preventDefault();
//});

$('#formid').on('focusout', function(){
     save();
});

function save() {
    var url = $('#formid').attr('action');
    var data = $('#formid').serialize();
    $.ajax({
    	type: "POST",
      	url: url,
      	data: data,
//      	success:function(data) {
//      		if (typeof(data)!='undefined' && data !='') {
//      			alert(data);
//      		}
//      	}	
    });
}
 </script>

action
public function actionFail()
    {
        $model = new Activation();
        if ($model->load(Yii::$app->request->post())) {
            $model->save();
            return $this->redirect(['fail', 'id' => $model->active_id]);
        } else {
            return $this->render('fail', [
                'model' => $model,
            ]);
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-04-25
@alekskondr

You need to create an object of the inherited ActiveRecord class and work with it to save the data. You work with the form model, accept its data and try to save it here:
but what do you hope for if it is a form model (Model), and models of the ActiveRecord class work with the database and nothing more? Before that, you need to create a table in the database. And in the config, configure and connect the connection to the database.
Here is an article in the documentation on the topic, after which you can, I hope, process and save the data from the form.
But save them without reloading so that, just automatically when typing with ajax, as you wanted in your last banned question, you need to understand what my example from your last question does, but your knowledge base is not enough for this.
You need to read about javascript, ajax, jquery, learn all this and read the documentation for yii. And then do it. Probably, you are not familiar with php either, then you should also read the manual from php.net
. Good luck.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question