-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.js
82 lines (71 loc) · 2.88 KB
/
request.js
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
document.addEventListener("DOMContentLoaded", function () {
fetch('items.json')
.then(response => response.json())
.then(data => {
populateCategories(data.items);
})
.catch(error => console.error('Error fetching items:', error));
});
function populateCategories(items) {
const categorySelector = document.getElementById('categorySelector');
const categories = [...new Set(items.map(item => item.category))];
categories.forEach(category => {
const option = document.createElement('option');
option.value = category;
option.textContent = category;
categorySelector.appendChild(option);
});
// Populate items for the initial category
populateItems();
}
function populateItems() {
const categorySelector = document.getElementById('categorySelector');
const selectedCategory = categorySelector.value;
const itemSelector = document.getElementById('itemSelector');
fetch('items.json')
.then(response => response.json())
.then(data => {
itemSelector.innerHTML = '';
const filteredItems = data.items.filter(item => item.category === selectedCategory);
filteredItems.forEach(item => {
const option = document.createElement('option');
option.value = item.id;
option.textContent = item.name;
itemSelector.appendChild(option);
});
// Check if no items are available for the selected category
if (filteredItems.length === 0) {
const option = document.createElement('option');
option.textContent = 'No items available';
itemSelector.appendChild(option);
}
})
.catch(error => console.error('Error fetching items:', error));
}
document.getElementById('requestForm').addEventListener('submit', function (event) {
event.preventDefault();
const itemId = document.getElementById('itemSelector').value;
const peopleCount = document.getElementById('peopleCount').value;
// Get the current date and time
const createdAt = new Date().toISOString().slice(0, 19).replace('T', ' ');
fetch('submit_request.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
itemId: itemId,
peopleCount: peopleCount,
createdAt: createdAt // Add createdAt to the request body
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('Request submitted successfully!');
} else {
alert('Failed to submit request: ' + data.message);
}
})
.catch(error => console.error('Error submitting request:', error));
});