-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubmit_data.html
More file actions
98 lines (91 loc) · 3.2 KB
/
Copy pathsubmit_data.html
File metadata and controls
98 lines (91 loc) · 3.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Submission</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<h1>Submit Your Data</h1>
<form id="dataForm">
<label for="id">ID:</label>
<input type="text" id="id" name="id" required><br><br>
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<button type="submit">Submit</button>
</form>
<h2>Data from Server</h2>
<button id="refreshButton">Refresh Data</button>
<table id="dataTable">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<!-- Data will be inserted here -->
</tbody>
</table>
<script>
document.getElementById('dataForm').addEventListener('submit', function(event) {
event.preventDefault();
const id = document.getElementById('id').value;
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
fetch('http://localhost:3000/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: id, name: name, email: email })
})
.then(response => response.json())
.then(data => {
console.log(data);
fetchData();
})
.catch(error => console.error('Error:', error));
});
document.getElementById('refreshButton').addEventListener('click', function() {
fetchData();
});
function fetchData() {
fetch('http://localhost:3000/submit')
.then(response => response.json())
.then(data => {
const dataTable = document.getElementById('dataTable').getElementsByTagName('tbody')[0];
dataTable.innerHTML = ''; // Clear the table body
data.data.forEach(item => {
const row = dataTable.insertRow();
const cellId = row.insertCell(0);
const cellName = row.insertCell(1);
const cellEmail = row.insertCell(2);
cellId.textContent = item.id;
cellName.textContent = item.name;
cellEmail.textContent = item.email;
});
})
.catch(error => console.error('Error:', error));
}
// Fetch data initially when the page loads
fetchData();
</script>
</body>
</html>