-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_form.php
103 lines (85 loc) · 3.4 KB
/
simple_form.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Form</title>
</head>
<body>
<h1>Simple Form</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name" required><br><br>
<label for="last_name">Last Name:</label>
<input type="text" id="last_name" name="last_name" required><br><br>
<label for="other_names">Other Names:</label>
<input type="text" id="other_names" name="other_names"><br><br>
<label for="email">Email Address:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="phone">Phone Number:</label>
<input type="tel" id="phone" name="phone" pattern="[0-9]{10}" required><br><br>
<label for="gender">Gender:</label>
<select id="gender" name="gender" required>
<option value="">Select</option>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$errors = [];
$first_name = clean_input($_POST["first_name"]);
$last_name = clean_input($_POST["last_name"]);
$other_names = clean_input($_POST["other_names"]);
$email = clean_input($_POST["email"]);
$phone = clean_input($_POST["phone"]);
$gender = clean_input($_POST["gender"]);
if (empty($first_name) || empty($last_name)) {
$errors[] = "First name and last name are required";
}
if (strlen($first_name) < 1) {
$errors[] = "First name cannot be less than 1 character";
}
if (!empty($other_names) && !preg_match("/^[a-zA-Z ]*$/", $other_names)) {
$errors[] = "Other names can only contain letters and spaces";
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email format";
}
if (!preg_match("/^[0-9]{10}$/", $phone)) {
$errors[] = "Phone number must be 10 digits";
}
if (empty($gender)) {
$errors[] = "Gender is required";
}
if (empty($errors)) {
$data = [
'first_name' => $first_name,
'last_name' => $last_name,
'other_names' => $other_names,
'email' => $email,
'phone' => $phone,
'gender' => $gender
];
$json_data = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents('database.json', $json_data);
echo "<p>Data submitted successfully!</p>";
} else {
echo "<ul>";
foreach ($errors as $errors) {
echo "<li>$errors</li>";
}
echo "</ul>";
}
}
function clean_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
</body>
</html>