-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappointScript.js
More file actions
50 lines (42 loc) · 1.95 KB
/
appointScript.js
File metadata and controls
50 lines (42 loc) · 1.95 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
const form = document.getElementById("data-form");
const nameInput = document.getElementById("name");
const emailInput = document.getElementById("email");
const message = document.getElementById("message");
const dataList = document.getElementById("data-list");
form.addEventListener("submit", async (e) => {
e.preventDefault();
const data = {
name: nameInput.value,
email: emailInput.value,
};
try {
const response = await axios.post("https://crudcrud.com/api/1f263326b7064fef8eda679e10820694/allUserData", data);
console.log(response.data);
message.textContent = "Data saved successfully.";
// Clear input fields
nameInput.value = "";
emailInput.value = "";
// Fetch and display data
fetchAndDisplayData();
} catch (error) {
console.error(error);
message.textContent = "Error occurred while saving data.";
}
});
function fetchAndDisplayData() {
axios.get("https://crudcrud.com/api/1f263326b7064fef8eda679e10820694/allUserData")
.then((response) => {
dataList.innerHTML = ""; // Clear previous data
response.data.forEach((item) => {
const listItem = document.createElement("li");
listItem.textContent = `Name: ${item.name}, Email: ${item.email}`;
dataList.appendChild(listItem);
});
})
.catch((error) => {
console.error(error);
message.textContent = "Error occurred while fetching data.";
});
}
// Initial data fetch and display
fetchAndDisplayData();