Skip to content

Instantly share code, notes, and snippets.

@pranid
Last active April 17, 2018 07:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pranid/fc2c8b5625df2a8b7411e18ab0339753 to your computer and use it in GitHub Desktop.
PHP Ajax form controller Jquery
$(function () {
$.notifyDefaults({
placement: {from: "top", align: "center"},
animate: {enter: "animated fadeInUp", exit: "animated fadeOutDown"}
});
$('body').on('submit', 'form', function (e) {
e.preventDefault();
$('button[type=submit]').attr('disabled', true);
var data = new FormData($(this)[0]);
var form_url = $(this).attr('action');
if ($('[name=_method]').val() == "POST") {
var mest = 'created';
} else {
var mest = 'updated';
}
$.ajax({
url: form_url,
type: 'POST',
data: data,
dataType: 'json',
async: true,
cache: false,
contentType: false,
processData: false,
success: function (res) {
console.log(res);
if (res.success) {
$.notify({
title: '<strong>Success!</strong>',
message: 'Record has been ' + mest + ' successfully.'
}, {type: 'success'});
setTimeout(function () {
location.reload();
}, 500);
} else {
if (res.error) {
showError(res.error);
}
$('button[type=submit]').attr('disabled', false);
}
},
error: function (res) {
$.notify({
title: '<strong>Sorry!</strong>',
message: 'Record has not been ' + mest + '.'
}, {type: 'danger'});
$('button[type=submit]').attr('disabled', false);
}
});
});
$('.btn-delete').click(function (e) {
e.preventDefault();
var url = $(this).attr('href');
var r = confirm("Do you want to delete this record?");
if (r == true) {
$.ajax({
url: url, type: 'POST', dataType: 'json', success: function (e) {
if (e.success) {
$.notify({title: '<strong>Success!</strong>', message: e.msg}, {type: 'success'});
location.reload();
} else {
$.notify({title: '<strong>Sorry!</strong>', message: e.msg}, {type: 'danger'});
}
}, error: function (e) {
$.notify({
title: '<strong>Sorry!</strong>',
message: 'Record not has been deleted.'
}, {type: 'warning'});
}
});
}
});
function showError(errors) {
$.each(errors, function (index, val) {
$('[name=' + index + ']').closest('.form-group').addClass('has-error');
var ip_parent = $('[name=' + index + ']').closest('.form-group').children('div');
$.each(val, function (index, val) {
ip_parent.find('.error-container').html('<span class="clear-fix error text-danger"><span class="glyphicon glyphicon-exclamation-sign"></span> ' + val + '</span>')
});
});
}
$('.form-control, input').blur(function (event) {
if ($(this).val() != '') {
$(this).closest('.form-group').removeClass('has-error');
$(this).closest('.form-group').find('.error').remove();
$(this).closest('.form-group').addClass('has-success');
} else {
$(this).closest('.form-group').removeClass('has-error');
}
});
});
function editCharges(code) {
$('#form-container').css('opacity', '0.5');
$.get(location + '/edit/' + code, function (data) {
$('#form-container').html(data);
$('#form-container').css('opacity', '1');
});
}
function fillForm(form_data) {
$.each(form_data, function(index, val) {
var name_main = index;
$.each(val, function(index, val) {
$('[name="'+name_main+'['+index+']"]').val(val);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment