I am trying to check values
from a database through sending the values by for loop in ajax request to php file,
"each value in request" then the file return variable called "avl" if
so it is available if not it is not available.
$data["avl"]==1
The problem is that I check a stream of values
and they all must return 1
to continue my process, but the
condition doesn't wait until the for loop ends to check. It checks the condition before
the for loop starts, even the code is not like that. Ex: it does the condition in line
100 before for loop ends in line
50.
var cartItemContainer =
document.getElementsByClassName('cart-items')[0]
var cartRows =
cartItemContainer.getElementsByClassName('cart-row')
var avl_qty =
1;
for (var i = 0; i < cartRows.length; i++) {
var
cartItemContainer =
document.getElementsByClassName('cart-items')[0]
var cartRows =
cartItemContainer.getElementsByClassName('cart-row')
var cartRow =
cartRows[i]
var titleElement =
cartRow.getElementsByClassName('cart-item-title')[0]
var item =
titleElement.innerText
var quantityElement =
cartRow.getElementsByClassName('cart-quantity-input')[0]
var
freequantityElement =
cartRow.getElementsByClassName('cart-quantity-free-input')[0]
var
quantity = quantityElement.value
var freequantity =
freequantityElement.value
alert("before avilability
ajax")
$.ajax({
url: "checkavlqty.php",
method: "POST",
data: {
item: item,
quantity: quantity,
freequantity: freequantity
},
dataType: "JSON",
success: function(data) {
alert(JSON.stringify(data));
if (data["avl"] == 0) {
alert("inside
condistion")
avl_qty = 0;
}
}
})
}
alert(avl_qty)
It
always alerts 1
, even the final value of avl_qty is
0
.
No comments:
Post a Comment