-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.php
149 lines (136 loc) · 4.82 KB
/
home.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
session_start();
$mysqli = new mysqli(null,"root", "pass", "simple_todo_db", "3306");
if ($mysqli->connect_errno) {
echo "". $mysqli->connect_error;
exit();
}
if (isset($_SESSION["registering"]) && isset($_SESSION["name"]) && isset($_POST["password"])) {
$name = htmlspecialchars($_POST["name"]);
$password = htmlspecialchars($_POST["password"]);
if ($mysqli->query("SELECT * FROM todo_users WHERE name = '$name'")->num_rows > 0) {
$_SESSION["register-error"] = "Name already exists";
header("Location: register.php");
}
$mysqli->query("INSERT INTO `todo_users` (`name`, `password`) VALUES ('$name', '$password');");
} else if (isset($_SESSION["registering"])) {
$_SESSION["register-error"] = "Please input name and password!";
header("Location: register.php");
}
unset($_SESSION["registering"]);
if (isset($_POST["name"]) && isset($_POST["password"])) {
$name = htmlspecialchars($_POST["name"]);
$password = htmlspecialchars($_POST["password"]);
$sql = $mysqli->query("SELECT * FROM todo_users WHERE name = '$name' AND password = '$password'");
if ($sql->num_rows > 0) {
$row = $sql->fetch_assoc();
$_SESSION["id"] = $row["id"];
$_SESSION["name"] = $row["name"];
} else {
$_SESSION["login-error"] = "Invalid name or password";
header("Location: login.php");
}
} else if (!isset($_SESSION["id"])) {
header("Location: login.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Simple Todo App</title>
<link rel="stylesheet" href="public/css/style.css">
</head>
<body>
<div id="title">Simple To-Do List</div><br>
<div>
<?php
echo("Welcome, " . $_SESSION["name"] . "!<br>");
?>
<button id="logout-button" onclick="location.href='logout.php'">Logout</button>
</div>
<div id="task-input-container">
<form method="POST" action="add.php">
<span>
<input type="text" name="task-input" id="task-input" placeholder="What do you need to do?">
<button id="add-task-button" type="submit">Add</button>
<?php
if (isset($_SESSION["task-input"])) {
if ($_SESSION["task-input"] == "Invalid task input") {
echo "<span style='color: red;'>Invalid task input</span>";
} else if ($_SESSION["task-input"] == "Task added") {
echo "<span style='color: green;'>Task added</span>";
} else if ($_SESSION["task-input"] == "MySQL error") {
echo "<span style='color: red;'>".$_SESSION["task-input"]["desc"]."</span>";
}
unset($_SESSION["task-input"]);
}
?>
</span>
</form>
<form method="POST" enctype="multipart/form-data" action="import.php">
<span>
<input type="file" style="margin-top: 10px" name="import" id="import" accept=".csv">
<button id="import-button" type="submit">Import</button>
<?php
if (isset($_SESSION["import-error"])) {
echo "<span style='color: red;'>".$_SESSION["import-error"]."</span>";
unset($_SESSION["import-error"]);
}
?>
</span>
</form>
<button id="export-button" onclick="location.href='export.php'">Export</button>
</div>
<div id="task-list">
<?php
$sql = "SELECT * FROM tasks WHERE user_id = " . $_SESSION["id"];
$result = $mysqli->query($sql);
$ongoing = [];
$finished = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$status = $row['status'];
if ($status == false) {
$ongoing[] = $row;
} else {
$finished[] = $row;
}
}
}
if (count($ongoing) > 0) {
foreach ($ongoing as $task) {
echo "
<div id='ongoing-task'>
<input type='checkbox' class='task-status' data-id='{$task['id']}' data-status='{$task['status']}' " . ($task['status'] ? 'checked' : '') . ">
<span class='task-name'>{$task['name']}</span>
<div id='delete-task'>
<form method='POST' action='delete.php'>
<input type='hidden' name='id' value='{$task['id']}'>
<button id='delete-task-button' type='submit'>Delete</button>
</form>
</div>
</div>
";
}
}
if (count($finished) > 0) {
foreach ($finished as $task) {
echo "
<div id='finished-task'>
<input type='checkbox' class='task-status' data-id='{$task['id']}' data-status='{$task['status']}' " . ($task['status'] ? 'checked' : '') . ">
<span class='task-name'>{$task['name']}</span>
<div id='delete-task'>
<form method='POST' action='delete.php'>
<input type='hidden' name='id' value='{$task['id']}'>
<button id='delete-task-button' type='submit'>Delete</button>
</form>
</div>
</div>
";
}
}
?>
</div>
<script src="public/js/script.js"></script>
</body>
</html>