javascript - php jquery ajax jquery >than not working as it should -
i have form validated through below code:
function add_to_cart(){ jquery('#modal_errors').html(""); var quantity = jquery('#quantity').val(); var available=jquery('#available').val(); var error=''; var data = jquery('#add_product_form').serialize(); if(quantity==''||quantity==0){ error+='<p class="text-danger text-center">you must enter quantity greater 0!!! set order 0 kindly visit cart!!!</p>'; jquery('#modal_errors').html(error); return; }else if(!(quantity < available)){ error += '<p class="text-danger text-center">there '+available+' available, kindly enter number equal or smeller availability!!!</p>'; jquery('#modal_errors').html(error); return; }else{ jquery.ajax({ url : '/aresv2/admin/parsers/add_cart.php', method : 'post', data: data, success: function(){ location.reload(); }, error: function(){alert("something went wrong in cart parser!!");} }); }}
the else if statement
((!(quantity < available))) note tried make (quantity > available) no avail
is not working should not accepting numbers pass though within accepted range.
example quantity 2 , available 12. quantity 3 seen wrong (which should not seen wrong), 1 seen right (which correct) , 13 wrong (which correct)
the below jquery function getting data:
<form action="add_cart.php" method="post" data-toggle="validator" id="add_product_form"> <input type="hidden" name="product_id" value="<?= $id ;?>"> <?php if (isset($_session['sbuser'])){ ?> <div class="form-group"> <div class="col-xs-3"> <label for="quantity">quantity:</label> <input type="number" class="form-control" name="quantity" id="quantity" min="1"> </div><div class="col-xs-9"></div> <p>available: <?= $prodqty['prodqty'] ;?></p> <!-- <p>available: <?= $prodtype ;?></p> --> </div><br /><br /> <?php }else{ echo'<b>kindly, login clicking <a href="login.php"><u>here</u></a> able add items shopping cart!!'; } ;?> <input type="hidden" name="available" id="available" value="<?= $prodqty['prodqty'] ;?>"> </form>
can please help.
try out should work you. jquery's .val()
method returns string i.e "5"
misbehaves when compare numbers.
function add_to_cart(){ jquery('#modal_errors').html(""); var quantity = +jquery('#quantity').val(); //added + convert number var available= +jquery('#available').val(); var error=''; var data = jquery('#add_product_form').serialize(); if(quantity===0){ error+='<p class="text-danger text-center">you must enter quantity greater 0!!! set order 0 kindly visit cart!!!</p>'; jquery('#modal_errors').html(error); return; }else if(quantity > available){ error += '<p class="text-danger text-center">there '+available+' available, kindly enter number equal or smeller availability!!!</p>'; jquery('#modal_errors').html(error); return; }else{ jquery.ajax({ url : '/aresv2/admin/parsers/add_cart.php', method : 'post', data: data, success: function(){ location.reload(); }, error: function(){alert("something went wrong in cart parser!!");} }); }}
Comments
Post a Comment