-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptExercises.js
More file actions
102 lines (76 loc) · 2.73 KB
/
scriptExercises.js
File metadata and controls
102 lines (76 loc) · 2.73 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
99
100
101
102
function getvariables(){
username = localStorage.getItem("username");
password = localStorage.getItem("password");
};
document.addEventListener("DOMContentLoaded", () => {
fetchData();
});
async function handleForm(event) {
event.preventDefault();
const exerciseName = document.getElementById('exercise-name').value;
const weight = document.getElementById('weight').value;
const sets = document.getElementById('sets').value;
const reps = document.getElementById('reps').value;
username = localStorage.getItem("username");
try {
const response = await fetch('exercises.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username: username, exercise_name: exerciseName, weight: weight, sets:sets, reps: reps}),
});
} catch (error) {
console.error('Error:', error);
alert('Failed to connect to the server.');
}
fetchData();
}
async function handleFormDelete(event) {
event.preventDefault();
const exerciseName = document.getElementById('exercise-delete').value;
const dat = document.getElementById('dat').value;
username = localStorage.getItem("username");
try {
const response = await fetch('exercises.php', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username: username, exercise_name: exerciseName, date:dat}),
});
} catch (error) {
console.error('Error:', error);
alert('Failed to connect to the server.');
}
fetchData();
}
async function fetchData() {
const username = localStorage.getItem("username");
try {
const url = `exercises.php?username=${encodeURIComponent(username)}`;
const response = await fetch(url, {
method: 'GET'
});
if (!response.ok) {
throw new Error('Error while getting data. Status: ' + response.status);
}
const data = await response.json();
const tableBody = document.querySelector("#data-table tbody");
tableBody.innerHTML = '';
data.forEach(item => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${item.date}</td>
<td>${item.exercise_name}</td>
<td>${item.weight}</td>
<td>${item.sets}</td>
<td>${item.reps}</td>
`;
tableBody.appendChild(row);
});
} catch (error) {
console.error('Error:', error);
alert('Failed to connect to the server.');
}
}