Last active
April 17, 2018 07:31
Star
You must be signed in to star a gist
PHP Ajax form controller Jquery
This file contains 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
$(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