35908-vm/note.php
2025-11-21 18:15:18 +00:00

85 lines
3.0 KiB
PHP

<?php
// note.php
require_once __DIR__ . '/db/config.php';
$pdo = db();
$note_date = $_GET['date'] ?? date('Y-m-d');
$message = '';
$note_content = '';
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$content = $_POST['content'] ?? '';
$date_to_save = $_POST['note_date'] ?? $note_date;
try {
$stmt = $pdo->prepare(
"INSERT INTO notes (note_date, content) VALUES (?, ?)
ON DUPLICATE KEY UPDATE content = VALUES(content)"
);
$stmt->execute([$date_to_save, $content]);
$message = 'Note saved successfully!';
} catch (PDOException $e) {
// In a real app, log this error instead of showing it to the user
$message = 'Error saving note: ' . $e->getMessage();
}
}
// Fetch existing note for the date
$stmt = $pdo->prepare("SELECT content FROM notes WHERE note_date = ?");
$stmt->execute([$note_date]);
$note = $stmt->fetch();
if ($note) {
$note_content = $note['content'];
}
$formatted_date = date("l, F j, Y", strtotime($note_date));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Note for <?= htmlspecialchars($formatted_date) ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap" rel="stylesheet">
</head>
<body>
<header class="header-gradient text-white text-center">
<div class="container">
<h1>My Diary</h1>
</div>
</header>
<main class="container mt-5">
<div class="card p-4">
<h2 class="mb-4"><?= htmlspecialchars($formatted_date) ?></h2>
<?php if ($message): ?>
<div class="alert alert-success" role="alert">
<?= htmlspecialchars($message) ?>
</div>
<?php endif; ?>
<form method="POST" action="note.php?date=<?= htmlspecialchars($note_date) ?>">
<input type="hidden" name="note_date" value="<?= htmlspecialchars($note_date) ?>">
<div class="mb-3">
<textarea class="form-control" name="content" id="noteContent" rows="15" placeholder="Start writing your thoughts..."><?= htmlspecialchars($note_content) ?></textarea>
</div>
<div class="d-flex justify-content-between">
<a href="index.php" class="btn btn-secondary">Back to Dashboard</a>
<button type="submit" class="btn btn-primary">Save Note</button>
</div>
</form>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>