-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
79 lines (70 loc) · 2.67 KB
/
index.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
<?php include 'inc/header.php'; ?>
<?php
$name = $email = $body = '';
$nameErr = $emailErr = $bodyErr = '';
// Form submit
if (isset($_POST['submit'])) {
// Validate name
if (empty($_POST['name'])) {
$nameErr = 'Name is required';
} else {
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
}
// Validate email
if (empty($_POST['email'])) {
$emailErr = 'Email is required';
} else {
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
}
// Validate body
if (empty($_POST['body'])) {
$bodyErr = 'Feedback is required';
} else {
$body = filter_input(INPUT_POST, 'body', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
}
if (empty($nameErr) && empty($emailErr) && empty($bodyErr)) {
// Add to database
$sql = "INSERT INTO feedback (name, email, body) VALUES ('$name', '$email', '$body')";
if (mysqli_query($conn, $sql)) {
// Success
header('Location: feedback.php');
} else {
// Error
echo 'Error: ' . mysqli_error($conn);
}
}
}
?>
<img src="img/logo.png" class="w-25 mb-3" alt="Logo">
<h2>Feedback</h2>
<p class="lead text-center">Leave feedback for Traversy Media</p>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" class="mt-4 w-75" method="POST">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control <?php echo !$nameErr ?: 'is-invalid'; ?>" id="name" name="name"
placeholder="Enter your name">
<div class="invalid-feedback">
<?php echo $nameErr; ?>
</div>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control <?php echo !$emailErr ?: 'is-invalid'; ?>" id="email" name="email"
placeholder="Enter your email">
<div class="invalid-feedback">
<?php echo $emailErr; ?>
</div>
</div>
<div class="mb-3">
<label for="body" class="form-label">Feedback</label>
<textarea class="form-control <?php echo !$bodyErr ?: 'is-invalid'; ?>" id="body" name="body"
placeholder="Enter your feedback" rows="5"></textarea>
<div class="invalid-feedback">
<?php echo $bodyErr; ?>
</div>
</div>
<div class="mb-3">
<input type="submit" name="submit" value="Send" class="btn btn-dark w-100">
</div>
</form>
<?php include 'inc/footer.php'; ?>