MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
CS311 ASSIGNMENT 2 SOLUTION SPRING 2025
Due Date: 20-June-2025
Total Marks: 20
DO NOT COPY PASTE THE SAME
FOR PAID SOLUTION CONTACT US
WHATSAPP 03162965677
Assignment Submission Instruction:
.DOC/DOCX word format file is required to submit on LMS
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product Management System</title>
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
<style>
body {
margin: 20px;
font-family: Arial, sans-serif;
}
table {
border-collapse: collapse;
width: 100%;
margin-bottom: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.form-container {
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
margin-bottom: 20px;
}
.form-container input {
margin: 5px;
padding: 5px;
}
button {
padding: 5px 10px;
margin: 5px;
}
</style>
</head>
<body>
<h1>Product Management System</h1>
<div class="section">
<h2>Display All Products</h2>
<button id="displayBtn">Display Products</button>
<div id="productTable"></div>
</div>
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
<div class="section">
<h2>Add New Product</h2>
<div class="form-group">
<label for="newId">ID:</label>
<input type="text" id="newId" required>
</div>
<div class="form-group">
<label for="newName">Name:</label>
<input type="text" id="newName" required>
</div>
<div class="form-group">
<label for="newCategory">Category:</label>
<input type="text" id="newCategory" required>
</div>
<div class="form-group">
<label for="newPrice">Price:</label>
<input type="number" id="newPrice" step="0.01" required>
</div>
<button id="addBtn">Add Product</button>
<p id="addMessage"></p>
</div>
<div class="section">
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
<h2>Delete Product</h2>
<div class="form-group">
<label for="deleteId">Product ID:</label>
<input type="text" id="deleteId" required>
</div>
<button id="deleteBtn">Delete Product</button>
<p id="deleteMessage"></p>
</div>
<script>
// Global variable to store the XML document
let xmlDoc;
// Load the XML file when the page loads
window.onload = function() {
loadXML();
};
// Function to load the XML file
function loadXML() {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
xmlDoc = this.responseXML;
}
};
xhttp.open("GET", "products.xml", true);
xhttp.send();
}
// Display all products
document.getElementById("displayBtn").addEventListener("click", function() {
if (!xmlDoc) {
alert("XML data not loaded yet. Please try again.");
return;
}
const products = xmlDoc.getElementsByTagName("Product");
let tableHTML =
"<table><tr><th>ID</th><th>Name</th><th>Category</th><th>Price</th></tr>";
for (let i = 0; i < products.length; i++) {
const id = products[i].getElementsByTagName("ID")[0].textContent;
const name = products[i].getElementsByTagName("Name")[0].textContent;
const category = products[i].getElementsByTagName("Category")[0].textContent;
const price = products[i].getElementsByTagName("Price")[0].textContent;
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
tableHTML +=
`<tr><td>${id}</td><td>${name}</td><td>${category}</td><td>$${price}</td></tr>`;
}
tableHTML += "</table>";
document.getElementById("productTable").innerHTML = tableHTML;
});
// Add a new product
document.getElementById("addBtn").addEventListener("click", function() {
if (!xmlDoc) {
alert("XML data not loaded yet. Please try again.");
return;
}
const newId = document.getElementById("newId").value.trim();
const newName = document.getElementById("newName").value.trim();
const newCategory = document.getElementById("newCategory").value.trim();
const newPrice = document.getElementById("newPrice").value.trim();
if (!newId || !newName || !newCategory || !newPrice) {
document.getElementById("addMessage").textContent = "Please fill in all fields.";
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
document.getElementById("addMessage").style.color = "red";
return;
}
// Check if ID already exists
const products = xmlDoc.getElementsByTagName("Product");
for (let i = 0; i < products.length; i++) {
const id = products[i].getElementsByTagName("ID")[0].textContent;
if (id === newId) {
document.getElementById("addMessage").textContent = "Product with this ID
already exists.";
document.getElementById("addMessage").style.color = "red";
return;
}
}
// Create new product node
const root = xmlDoc.getElementsByTagName("Products")[0];
const newProduct = xmlDoc.createElement("Product");
const idNode = xmlDoc.createElement("ID");
idNode.textContent = newId;
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
const nameNode = xmlDoc.createElement("Name");
nameNode.textContent = newName;
const categoryNode = xmlDoc.createElement("Category");
categoryNode.textContent = newCategory;
const priceNode = xmlDoc.createElement("Price");
priceNode.textContent = newPrice;
newProduct.appendChild(idNode);
newProduct.appendChild(nameNode);
newProduct.appendChild(categoryNode);
newProduct.appendChild(priceNode);
root.appendChild(newProduct);
// Save the updated XML back to the file
saveXML();
document.getElementById("addMessage").textContent = "Product added successfully!";
document.getElementById("addMessage").style.color = "green";
// Clear input fields
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
document.getElementById("newId").value = "";
document.getElementById("newName").value = "";
document.getElementById("newCategory").value = "";
document.getElementById("newPrice").value = "";
});
// Delete a product
document.getElementById("deleteBtn").addEventListener("click", function() {
if (!xmlDoc) {
alert("XML data not loaded yet. Please try again.");
return;
}
const deleteId = document.getElementById("deleteId").value.trim();
if (!deleteId) {
document.getElementById("deleteMessage").textContent = "Please enter a product
ID.";
document.getElementById("deleteMessage").style.color = "red";
return;
}
const products = xmlDoc.getElementsByTagName("Product");
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
let found = false;
for (let i = 0; i < products.length; i++) {
const id = products[i].getElementsByTagName("ID")[0].textContent;
if (id === deleteId) {
const root = xmlDoc.getElementsByTagName("Products")[0];
root.removeChild(products[i]);
found = true;
break;
}
}
if (found) {
// Save the updated XML back to the file
saveXML();
document.getElementById("deleteMessage").textContent = "Product deleted
successfully!";
document.getElementById("deleteMessage").style.color = "green";
document.getElementById("deleteId").value = "";
} else {
document.getElementById("deleteMessage").textContent = "Product with this ID not
found.";
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
document.getElementById("deleteMessage").style.color = "red";
}
});
function saveXML() {
const serializer = new XMLSerializer();
const xmlString = serializer.serializeToString(xmlDoc);
console.log("Updated XML:", xmlString);
const xhttp = new XMLHttpRequest();
xhttp.open("POST", "save_xml.php", true);
xhttp.setRequestHeader("Content-type", "application/xml");
xhttp.send(xmlString);
*/
}
</script>
</body>
</html>
OUTPUT SCREENSHOT
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677
MORE SOLUTIONS, HANDOUTS AND PAST PAPERS FREELY VISIT
VUAnswer.pk
REGARD - SARIM
WHATSAPP +923162965677
PLEASE NOTE:
Don't copy-paste the same answer.
Make sure you can make some changes to your solution file before
submitting copy paste solution will be marked zero.
If you found any mistake then correct yourself and inform me.
Before submitting an assignment must check your assignment requirement
file.
If you need some help or question about file and solutions feel free to ask.
FOR FREE ASSIGNMENTS SOLUTIONS VISIT
VUAnswer.pk
FOR PAID ASSIGNMENTS CORRECT SOLUTION CONTACT
WhatsApp: +923162965677