<?php
include('../includes/header.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo("<h1>Simple To-Do List</h1>");
// file location
$file = '../docs/to_do.txt';
// get values
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$task = isset($_POST['task']) ? $_POST['task'] : '';
// clean input
$task = htmlentities($task);
// create file if it doesn't exist
if (!file_exists($file))
{
$handle = fopen($file, 'w');
fclose($handle);
}
// STEP 0 → show form
if ($step == 0)
{
?>
<form action="to_do_list.php" method="POST">
<p>Enter a task:</p>
<input type="text" name="task" size="30">
<br><br>
<input type="hidden" name="step" value="1">
<input type="submit" value="Add Task">
</form>
<?php
}
// STEP 1 → write + read
else
{
// CLEAR LIST
if (isset($_POST['clear']))
{
file_put_contents($file, '');
echo("<p><strong>List cleared!</strong></p>");
}
// WRITE (append with timestamp)
if (!empty($task))
{
$handle = fopen($file, 'a');
fwrite($handle, $task . "\n");
fclose($handle);
echo("<p><strong>Task added!</strong></p>");
}
// READ
echo("<h3>Your To-Do List:</h3>");
echo("<p><em>Tasks are saved in a text file.</em></p>");
$handle = fopen($file, 'r');
if ($handle)
{
while (!feof($handle))
{
$line = fgets($handle);
if (!empty(trim($line)))
{
echo("<p>- $line</p>");
}
}
fclose($handle);
}
?>
<form action="to_do_list.php" method="POST">
<input type="hidden" name="step" value="0">
<input type="submit" value="Add Another Task">
</form>
<br>
<form action="to_do_list.php" method="POST">
<input type="hidden" name="step" value="1">
<input type="hidden" name="clear" value="1">
<input type="submit" value="Clear List">
</form>
<?php
}
include('../includes/footer.php');
?>