36432-vm/create_note.php
Flatlogic Bot 4ff4f98255 Basic
2025-11-28 15:28:25 +00:00

76 lines
2.7 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// If the user is not logged in, redirect to the login page.
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'] ?? '';
$content = $_POST['content'] ?? '';
$color = $_POST['color'] ?? '#FFDDC1';
$tags = $_POST['tags'] ?? '';
$user_id = $_SESSION['user_id'];
if (empty($title)) {
$error = 'Title is required.';
} else {
try {
$db = db();
$stmt = $db->prepare('INSERT INTO notes (user_id, title, content, color, tags) VALUES (?, ?, ?, ?, ?)');
$stmt->execute([$user_id, $title, $content, $color, $tags]);
header('Location: index.php?note_created=1');
exit;
} catch (PDOException $e) {
$error = 'Database error: ' . $e->getMessage();
}
}
}
?>
<?php require_once 'header.php'; ?>
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-body">
<h3 class="card-title text-center">Create a New Note</h3>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form action="create_note.php" method="POST">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<label for="content" class="form-label">Content</label>
<textarea class="form-control" id="content" name="content" rows="5"></textarea>
</div>
<div class="mb-3">
<label for="color" class="form-label">Color</label>
<input type="color" class="form-control form-control-color" id="color" name="color" value="#FFDDC1">
</div>
<div class="mb-3">
<label for="tags" class="form-label">Tags (comma-separated)</label>
<input type="text" class="form-control" id="tags" name="tags">
</div>
<button type="submit" class="btn btn-primary w-100">Create Note</button
</form>
</div>
</div>
</div>
</div>
<?php require_once 'footer.php'; ?>