i have an appstudio form that in theory is supposed to take the input date of an item's expiration and return a message in the label control in orange if date is within 5 days and red if within 3 days. at this time the code runs without error codes but does not change color for the label.
controls include: an input control for date 'inptDate' label that changes color and text to show days remaining 'lblShowExp' a submit button 'btnSubmit' a label that shows error codes related to
here is the code i tried:
let pw = 'BIA375'
let myDatabase = 'fmt17871'
let allFoods = {}
let results
btnSubmit.onclick = function() {
query = "SELECT ExpirationDate FROM ingredients WHERE ingredientName='Apples'"
req = Ajax("https://ormond.creighton.edu/courses/375/ajax-connection.php", "POST", "host=ormond.creighton.edu&user=" + netID + "&pass=" + pw + "&database=" + myDatabase + "&query=" + query)
if (req.status == 200) { // trip to and from database server worked
console.log(`req.responseText is a JSON string that looks like this: ${req.responseText}`)
results = JSON.parse(req.responseText)
if (results.length == 0) // no results were returned by the query
lblErrors.value = "There are no foods in the database."
else { // results were returned by the query
let dataToDisplay = ""
for (i = 0; i < results.length; i++)
dataToDisplay = dataToDisplay + results[i][4] + "\n"
allFoods = results
}
} else // trip to server DIDN'T work
lblErrors.value = "Error code: " + req.status
}
// Function to handle the submit button click event
btnSubmit.onclick = function() {
// Get the input date and parse it as a Date object
const inputDate = new Date(inptDate.value);
const currentDate = new Date();
// Calculate the difference between the input date and the current date in days
const dateDiff = Math.ceil((inputDate - currentDate) / (1000 * 60 * 60 * 24));
// Check if the input date is within 5 or 3 days of the current date
if (dateDiff <= 5 && dateDiff > 0) {
const foodItem = 'Food item'; // Replace with the actual food item name
lblShowExp.text = `${foodItem} expires in ${dateDiff} days`;
// Set the text color to orange if the difference is less than or equal to 5 days
if (dateDiff <= 5 && dateDiff > 3) {
lblShowExp.textColor = 'orange';
}
// Set the text color to red if the difference is less than or equal to 3 days
else if (dateDiff <= 3) {
lblShowExp.textColor = 'red';
}
} else {
lblShowExp.text = '';
}
};