-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.js
32 lines (29 loc) · 971 Bytes
/
post.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
document.addEventListener("DOMContentLoaded", function () {
const form = document.getElementById("rideForm");
form.addEventListener("submit", function (e) {
e.preventDefault();
const formData = new FormData(form);
const rideData = Object.fromEntries(formData.entries());
// Send ride data to the server
fetch("http://localhost:3000/saveRides", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(rideData),
})
.then((response) => {
if (!response.ok) throw new Error("Network response was not ok");
return response.text();
})
.then((message) => {
console.log(message);
alert("Ride published successfully!");
form.reset();
})
.catch((error) => {
console.error("There was a problem with the fetch operation:", error);
alert("Error publishing ride: " + error.message);
});
});
});