I am running a query to see if a value entered by the user exists in the database column or not. If it does not exist then I would like to display a hidden text and hide an image. I tried the below 2 codes but it does not work.
What exactly does not work? Please provide more details about what happens.
As for the code:
You need to handle the case when item is not found - so the variable Item will not be undefined
In general it is possible that an error occurs when you code is executed - you can try to debug it by wrapping the code within try-catch block and printing the error. Then you can test it in the Preview mode and check the output in the console.
So your second code example would look this way:
@shantanukumar847 Thanks, I appreciate this is the calculation for the actual price discount. I now have everything working apart form this calculation, would you be able to help with the code? I appreciate if it is your own and if you don’t want to disclose. I have been getting along well with the coding but arithmetic and application are a new venture for me! I have the following code so far but is obviously not working…
function getDis (results) {
let Item = results.items[0];
if (Item && Item.discountCode === $w("#coupon").value){
($w("#amount").value = $w("#amountText").text * String(Item.value) / 100);
}
}
function decideDiscount(results) {
let Item = results.items[0];
if (Item && Item.discountCode === $w("#coupon").value) {
$w("#invalidCode").hide(); //hide invalid code alert
$w("#discount").text = String(Item.value); //get the value ready to calculate it
$w("#discountbox").text = String(Item.value) + "% DISCOUNT APPLIED"; ////put the discount value in a text to show it to the user
getDis(); //calculate and update total
} else {
$w("#invalidCode").show();
$w("#discountLoader").hide();
}
}
async function getDis() {
let valueAfterDiscount = (Number($w("#finalAmount").text)) - ((Number($w("#finalAmount").text/100)) * (Number($w('#discount').text)));
await $w("#discountbox").show();
$w("#discountLoader").hide();
$w("#couponGo").disable();
$w('#finalAmount').text = parseFloat(String(valueAfterDiscount)).toFixed(2);
}