-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-require-validate
More file actions
145 lines (123 loc) · 3.97 KB
/
form-require-validate
File metadata and controls
145 lines (123 loc) · 3.97 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//All about form validation html, php
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"></form>
<?php
//Validation
//Define variable and set empty values
$name = $email = $gender = $comment = $website = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$gender = test_input($_POST["gender"]);
$comment = test_input($_POST["comment"]);
$website = test_input($_POST["website"]);
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
//Required*
//define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if($_POST["REQUEST_METHOD"] == "POST"){
if(empty($_POST["name"])){
$nameErr = "Name is required";
}else {
$name = test_input($_POST["name"]);
}
if(empty($_POST["email"])){
$emailErr = "Email is required";
}else{
$email = test_input("email");
}
if(empty($_POST["website"])){
$website = "";
} else{
$website = test_input($_POST["website"]);
}
if(empty($_POST["comment"])){
$comment = "";
}else{
$comment = test_input($_POST["comment"]);
}
if(empty($_POST["gender"])){
$genderErr = "You must select one";
}else{
$gender = test_input($_POST["name"]);
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Name: <input type="text" name="name">
<span class="error">*<?php echo $nameErr; ?></span></br>
Email: <input type="text" name="email">
<span class="error">*<?php echo $emailErr; ?></span></br>
Website: <input type="text" name="website">
<span class="error">*<?php echo $websiteErr;?></span></br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea></br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="other">Other
<span class="error">*<?php echo $genderErr; ?></span></br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
//define variables and set to empty values:
$nameErr= $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty($_POST["name"])){
$nameErr = "Name is required";
}else{
$name = test_input($_POST["name"]);
//check if name only contains letter and whitespace
if(!preg_match("/^[a-zA-Z-' ]*$/", $name)){
$nameErr = "Only letters and white space allowed";
}
}
if(empty($_POST["email"])){
$emailErr = "Email is Required";
}else{
$email = test_input($_POST["email"]);
//check if email address is well format
if(!filter_var($email, FILTER_VALIDATE_EMIL)){
$emailErr = "Invalid email format";
}
}
if(empty($_POST["website"])){
$website = "";
}else{
$website = test_input($_POST["website"]);
//check if URL address systax is valid
if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $website)){
$websiteErr = "Invalid URL";
}
}
if(empty($_POST["comment"])){
$comment = "";
}else{
$comment = test_input($_POST["comment"]);
}
if(empty($_POST["gender"])){
$genderErr = "Gender is required";
}else{
$gender = test_input($_POST["gender"]);
}
}
// Completed form
Name: <input type="text" name="name" value=<?php echo $name; ?>">
Email: <input type="text" name="email" value="<?php echo $email; ?>">
Website: <input type="text" name="website" value=<?php echo $website; ?>">
Comment: <textarea name="comment" rows="5" cols="40"<?php echo $comment; ?></textarea>
Gender:
<input type="radio" name="gender"
<?php if(isset($gender) $$ $gender=="female")echo "checked"; ?> value="female">Female
<input type="radio" name="gender"
<?php if(isset($gender) && $gender=="male") echo "checked";?> value="male">Male
<input type="radio" name="gender"
<?php if(isset($gender) && $gender=="other") echo "checked";?> value="other">Other