-
Notifications
You must be signed in to change notification settings - Fork 0
/
todos.php
130 lines (117 loc) · 4.51 KB
/
todos.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
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
<?php
// Initialize the edit mode flag
$edit_mode = false;
// Connect to the database
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myTodoList";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if the form has been submitted for adding or updating an item
if (isset($_POST["submit"])) {
if ($_POST["action"] === "create") {
// Retrieve the form data for adding an item
$title = $_POST["title"];
// Insert the data into the database
$sql = "INSERT INTO myTodoList (todoName) VALUES ('$title')";
if ($conn->query($sql) === TRUE) {
$status_message = "New todo item created successfully.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} elseif ($_POST["action"] === "update") {
// Retrieve the form data for updating an item
$id = $_POST["id"];
$title = $_POST["title"];
// Update the data in the database
$sql = "UPDATE myTodoList SET todoName='$title' WHERE id='$id'";
if ($conn->query($sql) === TRUE) {
$status_message = "Todo item updated successfully.";
} else {
echo "Error updating record: " . $conn->error;
}
}
}
// Check if an action has been requested
if (isset($_POST["action"])) {
if ($_POST["action"] === "edit") {
// Set edit mode flag and retrieve the todo item to edit
$edit_mode = true;
$id = $_POST["id"];
$sql = "SELECT * FROM myTodoList WHERE id='$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$title = $row["todoName"];
}
} elseif ($_POST["action"] === "delete") {
// Retrieve the todo item to delete
$id = $_POST["id"];
// Delete the data from the database
$sql = "DELETE FROM myTodoList WHERE id='$id'";
if ($conn->query($sql) === TRUE) {
$status_message = "Todo item deleted successfully.";
} else {
echo "Error deleting record: " . $conn->error;
}
}
}
// Retrieve all the todo items from the database
$sql = "SELECT * FROM myTodoList";
$result = $conn->query($sql);
// Output the todo items
?>
<?php
if (isset($status_message)) {
echo "<div class='status-message'>$status_message</div>";
}
?>
<?php if (!$edit_mode) { ?>
<form method="post">
<input type="text" name="title" placeholder="Add a new todo item...">
<input type="hidden" name="action" value="create">
<input type="submit" name="submit" value="Add Item">
</form>
<?php } else { ?>
<form method="post">
<input type="hidden" name="id" value="<?php echo $id ?>">
<input type="text" name="title" value="<?php echo $title ?>">
<input type="hidden" name="action" value="update">
<input type="submit" name="submit" value="Update Item">
</form>
<?php } ?>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<div class="todo-item">
<form method="post" class="checkbox-form">
<label>
<input type="checkbox" name="completed" value="<?php echo $row["id"]; ?>" <?php echo $row["completed"] ? 'checked' : ''; ?>>
<h2 class="<?php echo $row["completed"] ? 'completed' : ''; ?>"><?php echo $row["todoName"]; ?></h2>
</label>
</form>
<div class="button-container">
<form method="post" class="edit-form">
<input type="hidden" name="id" value="<?php echo $row["id"]; ?>">
<input type="hidden" name="action" value="edit">
<button type="submit">Edit</button>
</form>
<form method="post" class="delete-form">
<input type="hidden" name="id" value="<?php echo $row["id"]; ?>">
<input type="hidden" name="action" value="delete">
<button type="submit">Delete</button>
</form>
</div>
</div>
<?php
}
} else {
echo "<p>No todo items found.</p>";
}
// Close the database connection
$conn->close();
?>