<script>
async function loadData() {
var data = await fetch("Product.xml");
var parsedData = await data.text();
var parser = new DOMParser();
var Product_document = parser.parseFromString(parsedData,"text/xml");
var results = "";
var AlertBox = ""
var user_id_input = document.getElementById("user_id").value;
var todos = Product_document.getElementsByTagName("product");
for(var i = 0; i < todos.length; i++) {
var Name = todos[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue;
var Code = todos[i].getElementsByTagName("Code")[0].childNodes[0].nodeValue;
var Quantity = todos[i].getElementsByTagName("Quantity")[0].childNodes[0].nodeValue;
var Description = todos[i].getElementsByTagName("Description")[0].childNodes[0].nodeValue;
var Price = todos[i].getElementsByTagName("Price")[0].childNodes[0].nodeValue;
if(user_id_input === Code) {
results = "<div>"
+ "Code: " + Code
+ ",<br/> Name: " + Name
+ ", <br/>Quantity: " + Quantity
+ ",<br/> Description " + Description
+ ",<br/> Price " + Price
+ "</div><br/>";
AlertBox= "True";
}
if(AlertBox !== "True") {
alert("Error");
}
}
document.getElementById("results").innerHTML = results;
}
</script>
I'm in the process of developing a web application that takes input from users, parses an XML file, and then displays relevant information. This functionality is already working smoothly on my end.
In doing so, I encountered an issue where I wanted to implement an error alert when the user's input doesn't match any of the elements in the XML file. However, each time the app checks against an element that doesn't match the user input, I receive multiple error alerts unexpectedly. I am currently at a loss as to how to rectify this situation. see image description here
To troubleshoot, I attempted using a variable that would change to 'true' upon a valid match with the input, allowing only one error alert to show up if the variable remains 'false.' Unfortunately, even after implementing this approach, I still encounter the same issue with the error alert persisting. see image description here