I see two potential problems. The default action of the submit button might not technically be to submit the form. But it probably is, so let's not worry about that now.
The other problem is that since $.post() is asynchronous, it hasn't completed by the time you get here:
Code:
if(bad === false){ $('#cart-submit-form').submit(); }
So, the first thing I would try, is moving that into $.post() function itself, something like this:
Code:
$('#paypal-submit-button').click(function(e){
var bad = false, qtys = $('.qty-input').size();
e.preventDefault();
$('.qty-input').each(function(){
var qty = parseInt($(this).val());
var id = parseInt($(this).attr('prod_id'));
var qty_id = String($(this).attr('qty-input-num'));
$.post('check_avail_qty.php', {pid:id, quantity:qty}, function(data){
if(data == "good" && --qtys < 1)
{
$('#cart-submit-form').get(0).submit();
}
else
{
$('#prod-qty-check'+qty_id).html('Oh No! It seems we have run out of this item. Please remove it from your cart to continue.');
var bad = true;
alert(bad);
}
});
});
});
Note: In the highlighted line I added a .get(0), as this is the way to submit a form via javascript. However, if your intent was to trigger a jQuery submit event previously assigned to the form, it would be better to use either what you had (without the .get(0)) or to use the formal jQuery .trigger() function:
Code:
$('#cart-submit-form').trigger('submit');
Also, data == "good" might be too narrow. There might be white space. If there's a problem with the form not submitting even when quantities are sufficient, I'd use data.indexOf('good') > -1 instead:
Code:
if(data.indexOf('good') > -1 && --qtys < 1)
Bookmarks