Skip to content

Instantly share code, notes, and snippets.

@Gori4ka
Last active April 19, 2017 09:50
Show Gist options
  • Save Gori4ka/002ed2fadcdaac2fb5a60256f41b8082 to your computer and use it in GitHub Desktop.
Save Gori4ka/002ed2fadcdaac2fb5a60256f41b8082 to your computer and use it in GitHub Desktop.
WordPress call ajax
if (1) {
$('.main').on('submit', 'selector', function (e) {
var $form = $(this);
e.preventDefault();
wp.ajax.send('action_name', {
success: function (response) {
$('.alert', $form).addClass('succes').text(response.message);
$($form)[0].reset();
setTimeout(function () {
$('.alert', $form).removeClass('succes');
}, 5000)
},
error: function (response) {
$('.alert', $form).addClass('error').text(response.message);
},
data: $($form).serializeArray().postArrayToObject(),
});
});
}
if (1) {
$('selector').saiForm({
callbackSubmit: function (thisForm) {
wp.ajax.send('action_name', {
success: function (response) {
$('.alert', thisForm).addClass('succes').text(response.message);
$(thisForm)[0].reset();
setTimeout(function () {
$('.alert', thisForm).removeClass('succes');
}, 5000)
},
error: function (response) {
$('.alert', thisForm).addClass('error').text(response.message);
},
data: $(thisForm).serializeArray().postArrayToObject(),
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment