forked from abdurrahman-osman/node-user-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
116 lines (97 loc) · 4.29 KB
/
index.html
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html>
<head>
<title>Add User</title>
<!-- Include Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container text-center mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h3>Add User</h3>
</div>
<div class="card-body">
<form id="userForm" action="http://172.23.92.26/add" method="post">
<!-- Form fields go here, as in the previous example -->
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="form-group">
<label for="surname">Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary btn-block" id="submit">Add User</button>
</form>
</div>
</div>
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h3>Updated User List</h3>
</div>
<div class="card-body" id="userList">
<!-- User list will be displayed here after the GET request -->
</div>
</div>
</div>
</div>
</div>
<script>
const userForm = document.getElementById('userForm');
const userList = document.getElementById('userList');
const button = document.getElementById('submit')
// Function to fetch and display the updated user list
async function fetchUserList() {
try {
console.log("Fetching all users for reloading")
const response = await fetch('http://172.23.92.26/all');
const users = await response.json();
console.log(users)
// Display the updated user list
userList.innerHTML = '<ul>' + users.map(user => `<li>${user.name} ${user.surname} - ${user.email}</li>`).join('') + '</ul>';
} catch (error) {
console.error('An error occurred while fetching the user list:', error);
}
}
// Execute the fetchUserList function when the page loads
window.addEventListener('load', fetchUserList);
userForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(userForm);
const userData = {};
formData.forEach((value, key) => {
userData[key] = value;
});
// Construct a URL with query parameters
const url = new URL('http://172.23.92.26/add');
url.search = new URLSearchParams(userData).toString();
// Send a POST request with query parameters
try {
const response = await fetch(url, {
method: 'POST',
});
} catch (error) {
console.error('An error occurred:', error);
}
finally{
window.location.reload();
}
});
</script>
</body>
</html>