I'm trying to validate a form using an AJAX call to check available inventory. If the inventory check has an error it returns a msg, if not the form should submit.
I have the following code:
$("form[id*='distributor_']").submit(function(){
return checkAvailableInventory($(this));
});
function checkAvailableInventory(form) {
$.ajax({
url: "/ajax/quantity.php?" + form.serialize(),
success: function(msg) {
if (msg) {
alert(msg);
return false;
} else {
return true;
}
}
});
}
I suspect that this issue occurs due to the asynchronous nature of AJAX and that the success: clause doesn't fire until well after the checkAvailableInventory() life cycle is completed.
Does anyone have suggestions to solve this issue? I've seen some examples of people using timeouts but that seems like a workaround with possible problems.