-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.js
67 lines (65 loc) · 2.03 KB
/
form.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
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
const form = document.getElementById('newEmployeeForm');
form.addEventListener('submit', (event) => {
event.preventDefault();
const formData = new FormData(form);
const employee = {
name: formData.get('name'),
designation: formData.get('designation'),
phone_number: formData.get('phone_number'),
email: formData.get('email')
};
fetch('http://localhost:5500/addemployee', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(employee)
})
.then(response => {
if (!response.ok) {
throw new Error('Error adding employee');
}
return response.text();
})
.then(data => {
console.log(data);
form.reset();
location.reload(); // Reload the page to update the employee list
})
.catch(error => {
console.error('Error adding employee:', error);
});
});
const updateForm = document.getElementById('updateEmployeeForm');
updateForm.addEventListener('submit', (event) => {
event.preventDefault();
const formData = new FormData(updateForm);
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);
updateForm.reset();
location.reload(); // Reload the page to update the employee list
})
.catch(error => {
console.error('Error updating employee:', error);
});
});