T
T
teodor7teodor72016-06-23 16:28:34
Yii
teodor7teodor7, 2016-06-23 16:28:34

How to make AJAX submit and process data in Yii2 ActiveForm?

Tell me, you need to send Ajax data in ActiveForm after validation and then, after receiving the response, execute the function. The search was unsuccessful.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DieZz, 2016-06-23
@DieZz

More or less like this:

$form = ActiveForm::begin([
    'id' => 'form-input-example',
    'options' => [
       'onsubmit' => 'sendAjax(this, myAction)'
    ],
]);
...

sendAjax method:
var myAction = function (response) {
    //Делаем то, что нам нужно с ответом
    console.log(response);
}

function sendAjax(form, callback) {
    $.ajax({
        method: 'post',
        url: '/test',
        dataType: 'json',
        data: $(form).serialize()
    }).done(function (response) {
        callback(response);
    })

   //Возвращаем false чтобы форма не отправилась
   return false;
}

I haven't tested the code, but in theory it should work.

J
jStanly, 2019-06-15
@jStanly

Doesn't work well. The sendAjax() function works 2 times in a row, after which the page with the form is reloaded...(

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question