Skip to content

Instantly share code, notes, and snippets.

@mrofi
Created January 2, 2015 14:21
Show Gist options
  • Save mrofi/ff21235662e12c4a886f to your computer and use it in GitHub Desktop.
Save mrofi/ff21235662e12c4a886f to your computer and use it in GitHub Desktop.
Form Submitter JS
$('form').each(function() {
$(this).off().submit(function(e) {
e.preventDefault();
form = $(this);
url = form.attr('action');
icon = form.find('button[type=submit] > .icon');
icon.removeClass('hide');
form.find('.alert').addClass('hide');
form.find('.global-error').html('');
form.find('.form-group').removeClass('has-error');
form.find('.error').html('');
$.post(url, form.serialize(), function(success) {
icon.addClass('hide');
if (success == 'ok') {
form[0].reset();
form.find('.uploadPreview').attr('src', noImage);
form.find('[name=photo]').val(noImage);
form.find('.alert-success').removeClass('hide');
setTimeout("form.find('.alert-success').addClass('hide')", 2000);
form.find('input').not(':hidden').first().focus();
console.log('success');
form.parents('tr').prev().find('.btn-edit').click();
loadData();
} else {
form.find('.alert-danger').removeClass('hide');
for (x in success) {
if (x == 'global') form.find('.global-error').html(success[x]);
form.find('[name='+x+']').parents('.form-group').addClass('has-error');
form.find('[name='+x+']').siblings().find('.error').html(success[x]);
}
}
})
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment