I am working on fixing a website which is coded by someone else. Codes are really messy so I am afraid I can’t post it all here but I believe I provided enough information for you to see what could be wrong. Because at this point I am lost.
1. We get min and max limits
parent_id = '<?php echo $parent_id; ?>';
api = '<?php echo $api; ?>';
$.getJSON('getlimits.php', {'id': ""+parent_id+"", 'api': ""+api+""}, function(data) {
// Loop and assign Json (returned value) to our limit variables
$.each(data, function(key, val) {
min_limit = key;
max_limit = val;
});
});
getlimits.php OutPut:
{"10":"15000"}
2. We check the limits
amount = $('#quantity', this).val();
console.log(amount + ' - Max : ' + max_limit + ' Min : ' + min_limit);
if ( amount < min_limit) {
displayError("You can't order less than " + min_limit + " units",2000);
return false;
}
else if ( amount > max_limit ) {
displayError("You can't order more than " + max_limit + " units.",2000);
return false;
}
Logged Results;
800 - Max : 15000 Min : 10
I typed 800 units. It shouldn’t give any error but I am getting following error;
You can’t order more than 15000 units.
I am truly lost. Log shows correct values, getlimits.php returns correct values but if & else if conditions are not working.
I will be glad if anyone could help me out with this problem.
Thank you in advance.
amountis not an integer when you grab it through.val().So adjust this line:
to
As pointed out by Dennis, you will have to
parseIntthe values you are grabbing from the JSON as well. (min_limitandmax_limit).As Dennis also pointed out, you should add
10as a second parameter toparseIntto make sure it parses as a decimal number.So: