WP - Form Validation
WP - Form Validation
<head>
<style>
.error {
Color: red;
</style>
</head>
<body>
<?php
$ok = 1;
If ($_SERVER[“REQUEST_METHOD”] == “POST”) {
If (empty($_POST[“name”])) {
$ok = 0;
} else {
$name = test_input($_POST[“name”]);
If (!preg_match(“/^[a-zA-Z- ]+$/”,$name)) {
$ok = 0;
}
If (empty($_POST[“email”])) {
$ok = 0;
} else {
$email = test_input($_POST[“email”]);
If (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$ok = 0;
If (empty($_POST[“website”])) {
$website = “”;
} else {
$website = test_input($_POST[“website”]);
If (!filter_var($website, FILTER_VALIDATE_URL)) {
$ok = 0;
If (empty($_POST[“comment”])) {
$comment = “”;
} else {
$comment = test_input($_POST[“comment”]);
If (empty($_POST[“gender”])) {
$genderErr = “Gender is required”;
$ok = 0;
} else {
$gender = test_input($_POST[“gender”]);
Function test_input($data) {
$data = trim($data);
$data = htmlspecialchars($data);
Return $data;
?>
<br><br>
<br><br>
<br><br>
Gender:
<br><br>
</form>
<?php
Echo $gender;
?>
</body>
</html>