-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathact.js
53 lines (44 loc) · 1.89 KB
/
act.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
document.addEventListener("DOMContentLoaded", function() {
// Your script code here
// Form submission handler
const form = document.getElementById("petitionForm");
form.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = {
email: document.getElementById("email").value.trim(),
firstName: document.getElementById("firstName").value.trim(),
lastName: document.getElementById("lastName").value.trim(),
country: document.getElementById("country").value.trim(),
postalCode: document.getElementById("postalCode").value.trim(),
};
try {
const response = await fetch("http://localhost:3000/api/add-petition", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(formData),
});
const result = await response.json();
alert(result.message);
if (response.ok && result.message === "User registered successfully!") {
fetchRegisteredUserCount(); // Update the user count
}
} catch (err) {
console.error("Error occurred:", err);
alert("An error occurred. Please try again.");
}
});
// Fetch and display the registered user count
async function fetchRegisteredUserCount() {
try {
const response = await fetch("http://localhost:3000/api/registered-users");
const data = await response.json();
// Update the count on the page
const countElement = document.getElementById("userCount");
countElement.textContent = `People already signed: ${data.count}`;
} catch (err) {
console.error("Error fetching registered users count:", err);
}
}
// Call this function on page load to set initial count
fetchRegisteredUserCount();
});