-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
96 lines (76 loc) · 2.92 KB
/
index.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
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
<?php include '/my-php-code/to_do_list/config/database.php' ?>
<?php
//fetch from database
$statement = $pdo->prepare('SELECT * FROM tasks ORDER BY id DESC');
$statement->execute();
$todos = $statement->fetchAll(PDO::FETCH_ASSOC);
?>
<?php
$task = '';
$todoErr = '';
if (isset($_POST['submit'])) {
if (empty($_POST['task'])) {
$todoErr = 'Todo is required';
} else {
$task = filter_input(INPUT_POST, 'task', FILTER_SANITIZE_SPECIAL_CHARS);
}
//Adding to database
if (empty($todoErr)) {
$statement = $pdo->prepare("INSERT INTO tasks (Title)
VALUES (:Title ) ");
$statement->bindValue(':Title', $task);
$statement->execute();
header('Location: index.php');
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="./bootstrap-offline/css/bootstrap.css">
<script src="./bootstrap-offline/js/jquery-3.6.0.js"></script>
<script src="./bootstrap-offline/js/bootstrap.js"></script>
<title>To do list</title>
</head>
<body>
<div class="container d-flex flex-column align-items-center mt-5">
<h1><?php echo "Whoop it's " . date('l') . '!'; ?></h1>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" class="mt-4 d-flex flex-row w-75 justify-content-center" method="POST">
<!-- Name input -->
<div class="mb-3 w-100">
<input type="text" maxlength="50" class="form-control px-5 w-100 <?php echo $todoErr ? 'is-invalid' : null; ?>" id="task" name="task" placeholder="What do you want to do today ?...">
<div class="invalid-feedback">
<?php echo $todoErr ?>
</div>
</div>
<div class="mb-3 form-group">
<input type="submit" name="submit" value="Add" class="btn btn-dark w-100">
</div>
</form>
<?php if (empty($todos)) : ?>
<p class="lead mt-3">Add to your to do list</p>
<?php endif; ?>
<?php foreach ($todos as $item) : ?>
<div class='card mb-3 w-75 bg-primary'>
<div class='px-5 pt-3 text-left bg-light'>
<div class="d-flex flex-row justify-content-between">
<p class='font-italic font-weight-bold'><?php echo ucwords($item['Title']) ?></p>
<p class='font-italic'><?php echo ucwords($item['date']) ?></p>
</div>
<div class="mb-3">
<a href="update.php?id=<?php echo $item['id'] ?>" class="btn btn-sm btn-outline-primary">Edit</a>
<form method="post" action="delete.php" class="d-inline-block">
<input type="hidden" class="hidden" name='id' value="<?php echo $item['id'] ?>">
<button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
</form>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</body>
</html>