Need an help to solve the issue, got stuck and invested a lots of time.
What I am doing here,
I am submitting form using post traditional method but before the submitting the form, I am using the above jquery ajax code to do some operation and it's working fine.
Using this code when some one click on button, I want to show loader but it do not show because of using async:false
in jquery ajax. It shows the loader after completing the ajax success.
In this case it seems for there is nothing happening and no loader works just after click of button.
Html code for showing loader is like this
<button type="submit" id="submit" class="btn btn-primary add_campaign_handler_button" name="submit">
<i class="fa fa-spinner fa-spin" id="url_button_loader" style="display:none;"></i>
Save Settings
</button>
jQuery:
$("#submit").click(function() {
$('#url_button_loader').css('display', 'inline-block');
var submit_flag = "yes";
var where_to_appear_for_db = "";
/* create custom post url */
var post_name = $("#custom_post_slug").val();
if (post_name != "") {
var data = {
data: post_name
};
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
'action': 'create_custom_post_url',
'data': data
},
beforeSend: function() {
$('#url_button_loader').css('display', 'inline-block');
},
async: false,
success: function(response) {
if (response == "exists") {
Swal.fire({
type: 'error',
title: 'Oops...',
text: wpmlBackObj.smart_link_exists
});
$("#retargeting_url").val("");
submit_flag = "no";
$('html,body').animate({
scrollTop: 0
});
return;
}
$("#tooltip_error").hide();
$("#campaign_post_id_hidden").val(response);
get_retargeting_url(response, "");
}
});
if (submit_flag == "yes") {
return true;
} else {
$('#url_button_loader').css('display', 'none');
return false;
}
});
});