function ValidatePresence($fields_required) { global $errors; foreach ($fields_required as $field) { $value = trim($_POST[$field]); if (!HasPresence($value)) { $errors[$field] = FieldNameAsText($field) . " can't be blank"; } } }
<?php // require_once ("validation_functions.php"); function RedirectTo($new_location) { header("Location: " . $new_location); } $errors = array(); $message = ""; if (isset($_POST["submit"])) { $username = trim($_POST["username"]); $password = trim($_POST["password"]); //Validations $fields_required = array("username", "password"); foreach ($fields_required as $field) { $value = trim($_POST[$field]); if (!HasPresence($value)) { $errors[$field] = ucfirst($field) . " can't be blank"; } } $fields_with_max_lengths = array("username" => 10, "password" => 3); ValidateMaxLengths($fields_with_max_lengths); // Login if (empty($errors)) { if ($username == "Vera" && $password == "123") { RedirectTo("index.php"); } else { $message = "Username/password doesn't match"; } } } else { $username = "";
<!DOCTYPE html> <html> <head> <title>Validation Errors</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <?php require_once "validation_functions.php"; $errors = array(); $username = trim(""); if (!HasPresence($username)) { $errors["username"] = "******"; } echo FormErrors($errors); ?> </body> </html>