Cs311 Assignment Solution 2 by the Lost (2025)
Cs311 Assignment Solution 2 by the Lost (2025)
Subject: Cs311
Assignment No: 2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product Management</title>
<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 {
margin-bottom: 20px;
}
.form-container input {
margin: 5px;
padding: 5px;
}
button {
padding: 5px 10px;
margin: 5px;
}
</style>
</head>
<body>
<h2>Product Management</h2>
<div class="form-container">
<input type="text" id="newId" placeholder="Product ID">
<input type="text" id="newName" placeholder="Product Name">
<input type="text" id="newCategory" placeholder="Category">
<input type="number" id="newPrice" placeholder="Price">
<button onclick="addProduct()">Add Product</button>
</div>
<div class="form-container">
<input type="text" id="deleteId" placeholder="Product ID to Delete">
<button onclick="deleteProduct()">Delete Product</button>
</div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
</tr>
</thead>
<tbody id="productTableBody">
</tbody>
</table>
<script>
let xmlDoc;
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
xmlDoc = xhr.responseXML;
displayProducts();
}
};
xhr.send();
}
xmlDoc.documentElement.appendChild(newProduct);
// Clear form
document.getElementById("newId").value = "";
document.getElementById("newName").value = "";
document.getElementById("newCategory").value = "";
document.getElementById("newPrice").value = "";
displayProducts();
}
// Delete product
function deleteProduct() {
const id = document.getElementById("deleteId").value;
if (!id) {
alert("Please enter a product ID");
return;
}
if (!found) {
alert("Product ID not found");
} else {
document.getElementById("deleteId").value = "";
displayProducts();
}
}