Skip to content

Instantly share code, notes, and snippets.

@BJIAST
Created November 15, 2018 13:23
Show Gist options
  • Save BJIAST/7d3a7612f7acb202449e4abd81e49ecd to your computer and use it in GitHub Desktop.
Save BJIAST/7d3a7612f7acb202449e4abd81e49ecd to your computer and use it in GitHub Desktop.
ajax post
$('#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