-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
150 lines (140 loc) · 6.23 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Employee List</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<h1>Add Employee</h1>
<form id="newEmployeeForm">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br>
<label for="designation">Designation:</label><br>
<input type="text" id="designation" name="designation" required><br>
<label for="phone_number">Phone Number:</label><br>
<input type="tel" id="phone_number" name="phone_number" required><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br>
<input type="submit" value="Add Employee">
</form>
<h1>Employee List</h1>
<button id="fetchEmployeesButton">Fetch Employees</button>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Designation</th>
<th>Phone Number</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="employeeTableBody">
</tbody>
</table>
<h1>Update Employee</h1>
<form id="updateEmployeeForm" style="display:none;">
<input type="hidden" id="updateId" name="id">
<label for="updateName">Name:</label><br>
<input type="text" id="updateName" name="name" required><br>
<label for="updateDesignation">Designation:</label><br>
<input type="text" id="updateDesignation" name="designation" required><br>
<label for="updatePhoneNumber">Phone Number:</label><br>
<input type="tel" id="updatePhoneNumber" name="phone_number" required><br>
<label for="updateEmail">Email:</label><br>
<input type="email" id="updateEmail" name="email" required><br>
<input type="submit" value="Update Employee">
</form>
<script src="form.js"></script>
<script>
document.getElementById('fetchEmployeesButton').addEventListener('click', fetchEmployees);
function fetchEmployees() {
fetch('http://localhost:5500/getemployee')
.then(response => response.json())
.then(data => {
const tableBody = document.getElementById('employeeTableBody');
tableBody.innerHTML = ''; // Clearin the table before adding new data
data.forEach(employee => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${employee.id}</td>
<td>${employee.name}</td>
<td>${employee.designation}</td>
<td>${employee.phone_number}</td>
<td>${employee.email}</td>
<td>
<button onclick="editEmployee(${employee.id}, '${employee.name}', '${employee.designation}', '${employee.phone_number}', '${employee.email}')">Edit</button>
<button onclick="deleteEmployee(${employee.id})">Delete</button>
</td>
`;
tableBody.appendChild(row);
});
})
.catch(error => console.error('Error fetching employee data:', error));
}
function editEmployee(id, name, designation, phone_number, email) {
document.getElementById('updateId').value = id;
document.getElementById('updateName').value = name;
document.getElementById('updateDesignation').value = designation;
document.getElementById('updatePhoneNumber').value = phone_number;
document.getElementById('updateEmail').value = email;
document.getElementById('updateEmployeeForm').style.display = 'block';
}
function deleteEmployee(id) {
fetch(`http://localhost:5500/deleteemployee/${id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error('Error deleting employee');
}
return response.text();
})
.then(data => {
console.log(data);
fetchEmployees(); // Fetchin and refreshin the employee list
})
.catch(error => {
console.error('Error deleting employee:', error);
});
}
document.getElementById('updateEmployeeForm').addEventListener('submit', (event) => {
event.preventDefault();
const formData = new FormData(document.getElementById('updateEmployeeForm'));
const employee = {
id: formData.get('id'),
name: formData.get('name'),
designation: formData.get('designation'),
phone_number: formData.get('phone_number'),
email: formData.get('email')
};
fetch(`http://localhost:5500/updateemployee/${employee.id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(employee)
})
.then(response => {
if (!response.ok) {
throw new Error('Error updating employee');
}
return response.text();
})
.then(data => {
console.log(data);
document.getElementById('updateEmployeeForm').reset();
document.getElementById('updateEmployeeForm').style.display = 'none';
fetchEmployees(); // Fetch and refresh the employee list
})
.catch(error => console.error('Error updating employee:', error));
});
</script>
</body>
</html>