-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
130 lines (114 loc) · 2.88 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
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
<?php include './inc/header.php'; ?>
<?php
$name = $email = $body = ''; //SHORTHAND
$nameErr = $emailErr = $bodyErr = '';
//FORM VALIDATION
if(isset($_POST['submit'])){
//validate name
if(empty($_POST['name'])){
$nameErr = 'Name is required';
}else{
//MUST SANITIZE
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
}
//validate email
if(empty($_POST['email'])){
$emailErr = 'Email is required';
}else{
//MUST SANITIZE
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
}
//validate body
if(empty($_POST['body'])){
$bodyErr = 'Feedback is required';
}else{
//MUST SANITIZE
$body = filter_input(INPUT_POST, 'body', FILTER_SANITIZE_SPECIAL_CHARS);
}
if(empty($nameErr) && empty($emailErr) && empty($bodyErr)){
//MEANS NO ERROR
//SEND TO DB
$sql = "INSERT INTO feedback (name, email, body) VALUES
( '$name', '$email', '$body' ) ";
if(mysqli_query($conn, $sql)){
//SUCCESS
header('Location: feedback.php');
}else{
echo 'Error '. mysqli_error($conn);
}
}
}
?>
<!-- TO submit to the self in this case the index, we need to specify like this inside action. Or we can submit to other files as well. -->
<img src="./img/logo.png" style="width: 10rem;" alt="Feedback icon" />
<h2>Feedback</h2>
<p class="lead text-center">Leave us a feedback</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': null ;
?>
"
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': null ;
?>
"
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': null ;
?>
"
id="body"
name="body"
placeholder="Enter your feedback"
></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'; ?>