Created
November 15, 2018 13:23
-
-
Save BJIAST/7d3a7612f7acb202449e4abd81e49ecd to your computer and use it in GitHub Desktop.
ajax post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#formID').submit(function (e) { | |
let form = $(this), | |
data = $(this).serializeArray(), // сбор информации с полей | |
backendUrl = $(this).attr('action'); // если он в экшн, или руками написать | |
$.post(backendUrl, data).done(function (response) { | |
let result = JSON.parse(response); // принимай ответ с бекенда например -> echo json_encode(array('success' => true)) | |
if(result.success){ // | |
form.trigger('reset'); // очищаем поля | |
alert('Отправлено успешно!'); | |
}else{ | |
alert('Все поля должны быть заполнены!'); | |
} | |
}); | |
e.preventDefault(); // блокирует перезагрузку страницы | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment