-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
79 lines (74 loc) · 3.16 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 'includes/header.php'; ?>
<?php
$name = $email = $feedback = ""; // Initialize variable to avoid undefined variable error
$nameErr = $emailErr = $feedbackErr = ""; // This is a short-hand way of declaring variables with the same initial value
if (isset($_POST['send-submit'])) { // isset is a function that checks if a variable is set and is not NULL
if (empty($_POST['name'])) {
$nameErr = "Name is required";
} else {
// Filter the retrieved input to remove any unwanted characters
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
}
if (empty($_POST['email'])) {
$emailErr = "Email is required";
} else {
// Filter the retrieved input to remove any unwanted characters
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
}
if (empty($_POST['feedback'])) {
$feedbackErr = "Feedback is required";
} else {
// Filter the retrieved input to remove any unwanted characters
$feedback = filter_input(INPUT_POST, 'feedback', FILTER_SANITIZE_STRING);
}
if (empty($nameErr) && empty($emailErr) && empty($feedbackErr))
{
$query = 'INSERT INTO feedback_responses (name, email, feedback) VALUES (:name, :email, :feedback)';
$dbconnection = new DBConnection();
$connection = $dbconnection->getConnection();
$statement = $connection->prepare($query);
$result = $statement->execute(['name' => $name, 'email' => $email, 'feedback' => $feedback]);
/*
* Alternative:
$stmt = $connection->prepare($query);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':feedback', $feedback);
$result = $stmt->execute();
*/
if ($result) {
header('Location: feedback.php');
} else {
echo "Failed to submit feedback";
}
}
}
?>
<main>
<h2>Feedback</h2>
<p>Leave feedback for Bluebell University's Online Learning Setup</p>
<form action ="" method="POST">
<!-- We can also put the 'required' keyboard in input
so that I do not need to put a nameErr, emailErr, and feedbackErr-->
<label for="name">Name: </label>
<!-- Name attribute is so important because it is used to retrieve the
value of the input field-->
<input type="text" id="name" name="name" placeholder="Enter your name">
<div class="invalid-input">
<?php echo $nameErr; ?>
</div>
<label for="email">Email: </label>
<input type="email" id="email" name="email" placeholder="Enter your email">
<div class="invalid-input">
<?php echo $emailErr; ?>
</div>
<label for="feedback">Feedback: </label>
<textarea id="feedback" name="feedback" placeholder="Enter your feedback"></textarea>
<div class="invalid-input">
<?php echo $feedbackErr; ?>
</div>
<input type="submit" value="Send" name="send-submit">
</form>
</main>
<?php
include 'includes/footer.php'; ?>