-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.php
More file actions
139 lines (122 loc) · 5.14 KB
/
request.php
File metadata and controls
139 lines (122 loc) · 5.14 KB
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
<?php
/**
* Request a Book Page
* Allows logged-in users to request books not in the library
*/
$page_title = "Request a Book";
require_once 'includes/header.php';
require_once 'includes/functions.php';
// Check if user is logged in
if (!isLoggedIn()) {
header("Location: login.php?redirect=request.php");
exit();
}
$message = '';
$error = '';
// Handle book request form
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_request'])) {
$title = trim($_POST['title']);
$author = trim($_POST['author']);
$isbn = trim($_POST['isbn']);
$reason = trim($_POST['reason']);
$user_id = getCurrentUserId();
// Validation
if (empty($title) || empty($author) || empty($reason)) {
$error = "Title, author, and reason are required.";
} elseif (!empty($isbn) && !preg_match('/^(?:\d{10}|\d{13})$/', preg_replace('/[^0-9]/', '', $isbn))) {
$error = "Please enter a valid ISBN (10 or 13 digits).";
} else {
// Save to database
global $pdo;
$stmt = $pdo->prepare("INSERT INTO book_requests (user_id, title, author, isbn, reason) VALUES (?, ?, ?, ?, ?)");
if ($stmt->execute([$user_id, $title, $author, $isbn, $reason])) {
$message = "Your book request has been submitted successfully. Our team will review it.";
// Clear form values
$title = $author = $isbn = $reason = '';
} else {
$error = "Failed to submit your request. Please try again.";
}
}
}
?>
<h1>Request a Book</h1>
<div class="content-section">
<h2>Can't Find Your Book?</h2>
<p>Let us know about a book you'd like to see in our collection. Our team reviews all requests and considers adding popular titles.</p>
<?php if ($message): ?>
<div class="alert alert-success"><?php echo $message; ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-error"><?php echo $error; ?></div>
<?php endif; ?>
<form method="post" data-validate="true">
<div class="form-group">
<label for="title" class="form-label">Book Title *</label>
<input type="text" id="title" name="title" class="form-control"
data-validate="required" value="<?php echo isset($title) ? htmlspecialchars($title) : ''; ?>"
placeholder="Enter the book title">
</div>
<div class="form-group">
<label for="author" class="form-label">Author *</label>
<input type="text" id="author" name="author" class="form-control"
data-validate="required" value="<?php echo isset($author) ? htmlspecialchars($author) : ''; ?>"
placeholder="Enter the author's name">
</div>
<div class="form-group">
<label for="isbn" class="form-label">ISBN (Optional)</label>
<input type="text" id="isbn" name="isbn" class="form-control"
value="<?php echo isset($isbn) ? htmlspecialchars($isbn) : ''; ?>"
placeholder="Enter ISBN if known (10 or 13 digits)">
<small class="form-text">ISBN helps us locate the exact edition you're looking for.</small>
</div>
<div class="form-group">
<label for="reason" class="form-label">Reason for Request *</label>
<textarea id="reason" name="reason" rows="4" class="form-control"
data-validate="required" placeholder="Why would you like to see this book in our collection?"><?php echo isset($reason) ? htmlspecialchars($reason) : ''; ?></textarea>
</div>
<button type="submit" name="submit_request" class="btn btn-primary">Submit Request</button>
</form>
</div>
<div class="content-section">
<h2>Popular Requests</h2>
<p>See what books other members are requesting:</p>
<?php
// Get recent requests
global $pdo;
$stmt = $pdo->prepare("
SELECT br.*, u.name as user_name
FROM book_requests br
JOIN users u ON br.user_id = u.id
WHERE br.status = 'PENDING'
ORDER BY br.created_at DESC
LIMIT 5
");
$stmt->execute();
$recent_requests = $stmt->fetchAll();
?>
<?php if (!empty($recent_requests)): ?>
<table>
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Requested By</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($recent_requests as $request): ?>
<tr>
<td><?php echo htmlspecialchars($request['title']); ?></td>
<td><?php echo htmlspecialchars($request['author']); ?></td>
<td><?php echo htmlspecialchars($request['user_name']); ?></td>
<td><?php echo formatDate($request['created_at']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>No pending requests at the moment.</p>
<?php endif; ?>
</div>
<?php require_once 'includes/footer.php'; ?>