-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_order.html
More file actions
103 lines (89 loc) · 2.88 KB
/
process_order.html
File metadata and controls
103 lines (89 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Order</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 40px;
max-width: 500px;
}
input[type="text"], input[type="number"] {
width: 100%;
padding: 8px;
margin: 8px 0;
box-sizing: border-box;
}
button {
padding: 10px 15px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
margin-top: 10px;
}
#response {
margin-top: 20px;
font-weight: bold;
}
.item-group {
margin-bottom: 10px;
}
</style>
</head>
<body>
<h2>Order Form</h2>
<form id="orderForm">
<label for="customer">Customer Name:</label>
<input type="text" id="customer" name="customer" required>
<h3>Order Items</h3>
<div id="itemsContainer">
<div class="item-group">
<input type="text" placeholder="Item Name" class="itemName" required>
<input type="number" placeholder="Quantity" class="itemQty" min="1" required>
</div>
</div>
<button type="button" onclick="addItem()">Add Another Item</button><br>
<button type="submit">Submit Order</button>
</form>
<div id="response"></div>
<script>
function addItem() {
const container = document.getElementById('itemsContainer');
const div = document.createElement('div');
div.className = 'item-group';
div.innerHTML = `
<input type="text" placeholder="Item Name" class="itemName" required>
<input type="number" placeholder="Quantity" class="itemQty" min="1" required>
`;
container.appendChild(div);
}
document.getElementById('orderForm').addEventListener('submit', async function(event) {
event.preventDefault();
const customer = document.getElementById('customer').value;
const itemNames = Array.from(document.getElementsByClassName('itemName')).map(i => i.value);
const itemQtys = Array.from(document.getElementsByClassName('itemQty')).map(i => parseInt(i.value));
const items = itemNames.map((name, index) => ({
name: name,
qty: itemQtys[index]
}));
const payload = { customer, items };
try {
const response = await fetch('http://127.0.0.1:5000/api/orders', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
const result = await response.json();
document.getElementById('response').innerText = JSON.stringify(result, null, 2);
} catch (error) {
console.error('Error:', error);
document.getElementById('response').innerText = 'An error occurred while submitting the order.';
}
});
</script>
</body>
</html>