-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo_list.php
62 lines (55 loc) · 1.91 KB
/
todo_list.php
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
<?php
session_start();
include 'db.php';
$user_id = $_SESSION['user_id'];
// Handle adding a new task
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['task'])) {
$task = $_POST['task'];
$stmt = $conn->prepare("INSERT INTO user_tasks (user_id, task) VALUES (?, ?)");
$stmt->bind_param("is", $user_id, $task);
$stmt->execute();
$stmt->close();
}
// Handle deleting a task
if (isset($_GET['delete'])) {
$task_id = $_GET['delete'];
$stmt = $conn->prepare("DELETE FROM user_tasks WHERE id = ? AND user_id = ?");
$stmt->bind_param("ii", $task_id, $user_id);
$stmt->execute();
$stmt->close();
}
// Fetch tasks for the logged-in user
$stmt = $conn->prepare("SELECT * FROM user_tasks WHERE user_id = ? ORDER BY created_at DESC");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$tasks = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Personalized To-Do List</title>
<link rel="stylesheet" href="css/todo_list.css">
</head>
<body>
<div class="todo-container">
<h2>Your To-Do List</h2>
<form method="POST">
<input type="text" name="task" placeholder="New task..." required>
<button type="submit">Add Task</button>
</form>
<ul class="task-list">
<?php foreach ($tasks as $task): ?>
<li class="task-item">
<span><?= htmlspecialchars($task['task']); ?></span>
<a href="?delete=<?= $task['id']; ?>" class="delete-btn">Delete</a>
</li>
<?php endforeach; ?>
</ul>
<button id="back-home" onclick="location.href='welcome.php'">Back to Home Page</button>
</div>
</body>
</html>