-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-validation.php
More file actions
43 lines (37 loc) · 1.06 KB
/
form-validation.php
File metadata and controls
43 lines (37 loc) · 1.06 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
<?php
$name_error = $email_error = $choice_error = "";
$name = $email = $message = $success = $choice = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$name_error = "Name is required";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$name_error = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$email_error = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Invalid email format";
}
}
$errors = array($name_error, $email_error);
if (isset($_POST['Submit'])) {
if(!$errors){
header("view-users.php");
}
else {
echo "Unable to process form. Please try again";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}
?>