-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathindex.php
More file actions
246 lines (235 loc) · 7.84 KB
/
index.php
File metadata and controls
246 lines (235 loc) · 7.84 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Ma To-Do List</title>
<style>
body {
background: #f4f4f4;
font-family: Arial, sans-serif;
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
}
.container {
background: #fff;
padding: 40px 60px;
border-radius: 12px;
box-shadow: 0 4px 16px rgba(0,0,0,0.08);
text-align: center;
min-width: 350px;
}
h1 {
color: #3498db;
margin-bottom: 20px;
font-size: 2.2em;
letter-spacing: 2px;
}
#todo-form {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
#todo-input {
padding: 10px;
font-size: 1em;
border: 1px solid #ddd;
border-radius: 6px 0 0 6px;
outline: none;
width: 70%;
}
#add-btn {
padding: 10px 18px;
font-size: 1em;
border: none;
background: #3498db;
color: #fff;
border-radius: 0 6px 6px 0;
cursor: pointer;
transition: background 0.2s;
}
#add-btn:hover {
background: #217dbb;
}
ul {
list-style: none;
padding: 0;
}
li {
background: #f9f9f9;
margin-bottom: 10px;
padding: 12px 16px;
border-radius: 6px;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 1.1em;
box-shadow: 0 1px 4px rgba(0,0,0,0.03);
}
.delete-btn {
background: #e74c3c;
color: #fff;
border: none;
border-radius: 4px;
padding: 6px 12px;
cursor: pointer;
font-size: 0.95em;
transition: background 0.2s;
}
.delete-btn:hover {
background: #c0392b;
}
.empty {
color: #aaa;
font-style: italic;
margin-top: 20px;
}
.info-section {
margin-top: 30px;
padding: 18px 24px;
background: #eaf6fb;
border-radius: 8px;
color: #217dbb;
font-size: 1.05em;
}
footer {
margin-top: 40px;
color: #888;
font-size: 0.95em;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h1>my to-do list</h1>
<form id="todo-form" autocomplete="off" onsubmit="return false;">
<input type="text" id="todo-input" placeholder="Ajouter une tâche..." />
<button id="add-btn">Ajouter</button>
</form>
<ul id="todo-list"></ul>
<div id="empty-msg" class="empty">Aucune tâche pour le moment.</div>
<div class="info-section">
<strong>Astuce :</strong> Click "DELETE" to delete a task.<br>
Appuyez sur <kbd>Entrée</kbd> pour ajouter rapidement une tâche.<br>
<br>
Cette application ne sauvegarde pas les tâches après le rechargement de la page.<br>
Pour une version persistante, bla bla bla .
</div>
</div>
<footer>
Réalisé avec en PHP, HTML, CSS,et validation & Js.<br>
© <?php echo date('Y'); ?> Chafiq Hamza et youssef khalid
</footer>
<script>
const todoInput = document.getElementById('todo-input');
const addBtn = document.getElementById('add-btn');
const todoList = document.getElementById('todo-list');
const emptyMsg = document.getElementById('empty-msg');
function updateEmptyMsg() {
emptyMsg.style.display = todoList.children.length === 0 ? 'block' : 'none';
}
function renderTodo(id, text) {
const li = document.createElement('li');
li.textContent = text;
li.dataset.id = id;
const delBtn = document.createElement('button');
delBtn.textContent = 'Supprimer';
delBtn.className = 'delete-btn';
delBtn.onclick = () => {
fetch('todo_api.php?action=delete', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id })
}).then(res => res.json()).then(data => {
if (data.success) {
todoList.removeChild(li);
updateEmptyMsg();
updateCount();
}
});
};
li.appendChild(delBtn);
todoList.appendChild(li);
}
function getTodoCount() {
return todoList.children.length;
}
function resetTodos() {
// Supprime toutes les tâches côté client et côté serveur
const items = Array.from(todoList.children);
items.forEach(li => {
const id = li.dataset.id;
fetch('todo_api.php?action=delete', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id })
});
todoList.removeChild(li);
});
updateEmptyMsg();
updateCount();
}
addBtn.onclick = () => {
const text = todoInput.value;
if (!text.trim()) return;
fetch('todo_api.php?action=add', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text })
}).then(res => res.json()).then(data => {
if (data.success) {
renderTodo(data.id, text);
updateEmptyMsg();
updateCount();
}
});
todoInput.value = '';
todoInput.focus();
};
todoInput.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
addBtn.click();
}
});
// Ajout d'un bouton pour réinitialiser la liste
const resetBtn = document.createElement('button');
resetBtn.textContent = 'Tout effacer';
resetBtn.style.marginTop = '18px';
resetBtn.style.background = '#f39c12';
resetBtn.style.color = '#fff';
resetBtn.style.border = 'none';
resetBtn.style.borderRadius = '6px';
resetBtn.style.padding = '8px 18px';
resetBtn.style.cursor = 'pointer';
resetBtn.onclick = () => {
resetTodos();
};
document.querySelector('.container').appendChild(resetBtn);
// Affichage dynamique du nombre de tâches
const countDiv = document.createElement('div');
countDiv.style.marginTop = '12px';
countDiv.style.color = '#555';
countDiv.style.fontSize = '1em';
document.querySelector('.container').appendChild(countDiv);
function updateCount() {
countDiv.textContent = "Nombre de tâches : " + getTodoCount();
}
// Charger les tâches depuis la base de données au chargement de la page
function loadTodos() {
fetch('todo_api.php?action=list')
.then(res => res.json())
.then(todos => {
todoList.innerHTML = '';
todos.forEach(todo => renderTodo(todo.id, todo.text));
updateEmptyMsg();
updateCount();
});
}
loadTodos();
updateEmptyMsg();
updateCount();
</script>
</body>
</html>