I'm using Bootstrap v3.3.0 for my website. I'm using Bootstrap framework's Modal dialog functionality.
I've written one jQuery-AJAX function for form submission as follows:
$('#rebate_request_form').submit(function(e) {
$('#rebateModal').modal('hide');
$('#progressModal').modal('show');
var fileInput = $("#receipt_image")[0];
var input = $("#receipt_image").val();
var form = $(this);
var formdata = false;
if(window.FormData) {
formdata = new FormData(form[0]);
}
if(input != '') {
var ImgSizeInBytes = fileInput.files[0].size;
var filename = $('input[type=file]').val().split('\\').pop();
var customer_id = $('#customer_id').val();
}
if(input!='' && ImgSizeInBytes > 10485760) {
var trans_id = $('#trans_id').val();
var trans_date_dateLists_year_list = $("#trans_date_dateLists_year_list").val();
var trans_date_dateLists_month_list = $("#trans_date_dateLists_month_list").val();
var trans_date_dateLists_day_list = $("#trans_date_dateLists_day_list").val();
var params = "trans_id="+trans_id+"&trans_date_dateLists_day_list="+trans_date_dateLists_day_list+"&trans_date_dateLists_month_list="+trans_date_dateLists_month_list+"&trans_date_dateLists_year_list="+trans_date_dateLists_year_list+"&file_size="+ImgSizeInBytes+"&file_name="+filename+"&customer_id="+customer_id;
var method = 'GET';
} else {
var params = formdata ? formdata : form.serialize();
var method = 'POST';
}
var formAction = form.attr('action');
$.ajax({
url : 'rebate_request.php',
type : method,
cache : false,
data : params,
contentType : false,
processData : false,
success: function(response) {
$('#progressModal').modal('hide');
var responseObject = $.parseJSON(response);
if(responseObject.error_message) {
$('#rebateModal').modal('show');
if ($(".alert-dismissible")[0]) {
$('.alert-dismissible').remove();
}
var htmlString = "<div class='alert alert-danger alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>"+responseObject.error_message+"</div>";
$(htmlString).insertBefore('div.modal-body #rebate_request_form');
} else {
$('#successModal').modal('show');
}
}
});
e.preventDefault();
});
The above function is working properly but sometimes the modal with id "rebateModal" doesn't show up when error_message is received in response. Though sometimes in some browsers I don't encounter this issue, everything just works smoothly.
So I want to restructure my code regarding calling the modal methods using Bootstrap's function callbacks as follows:
$('#rebateModal').one('hidden.bs.modal', function () {
$('#progressModal').one('shown.bs.modal', function () {
//...more code...
}).modal('show');
}).modal('hide');
But I'm not able to write my code in above fashion since I'm a newbie to Bootstrap framework and JS.
So can someone please help me in restructuring the code I've written using Bootstrap's function callbacks?
If you need additional information regarding function callbacks in Bootstrap please refer below link: Link for Bootstrap Modal functionality
Thanks in advance.