Last active
August 29, 2015 14:05
-
-
Save yunchih/12672a151ae5fe463809 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ERROR | E_WARNING | E_PARSE); | |
/* | |
* Turn off notice so that empty array | |
* like $value and $error | |
* won't be taken as undefined array. | |
*/ | |
?> | |
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<style> | |
.error { | |
color: #FF0000; | |
} | |
.error:first-letter { | |
text-transform: uppercase; | |
} | |
</style> | |
</head> | |
<body> | |
<?php | |
$requiredField = array("name","email","gender"); | |
$checkValid = array( | |
"name"=> function($name){ | |
if (!preg_match("/^[a-zA-Z ]*$/",$name)) | |
return "Only letters and white space allowed"; | |
}, | |
"email"=> function($email){ | |
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) | |
return "Invalid email format"; | |
}, | |
"website"=> function($website){ | |
$pattern ="/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i"; | |
if (!preg_match($pattern,$website)) | |
return "Invalid URL"; | |
} | |
); | |
$error = array(); | |
$value = array(); | |
function getInput($field){ | |
if (empty($_POST[$field])) { | |
$input = ""; | |
} | |
else{ | |
$input = trim($_POST[$field]); | |
$input = stripslashes($input); | |
$input = htmlspecialchars($input); | |
} | |
return $input; | |
} | |
if ($_SERVER["REQUEST_METHOD"] == "POST") { | |
foreach ($requiredField as $field) { | |
$input = getInput($field); | |
if($input!=""){ | |
$value[$field] = $input; | |
$error[$field] = $checkValid[$field]($input); | |
var_dump($checkValid[$field]($input)); | |
/* | |
* The bug is here. | |
* The compiler says: | |
* Fatal error: Function name must be a string | |
* but the following expression passes: | |
* $error[$field] = $checkValid["name"]($input); | |
* | |
* Some minor observations: | |
* var_dump($field) ===> string(4) "name" | |
* //So $field is really a String!!!!! | |
* | |
* | |
* var_dump($checkValid[$field]) ===> | |
* object(Closure)#1 (1) { | |
* ["parameter"]=> array(1) { ["$name"]=> string(10) "" } | |
* } | |
* | |
* var_dump($checkValid[$field]($input)) ==> | |
* string(20) "Invalid email format" | |
* | |
*/ | |
} | |
else{ | |
$error[$field] = $field . " is required!"; | |
} | |
} | |
} | |
/* | |
* $value and $error preceded by "global" | |
* Such a weird scope rule never seen in other | |
* language. | |
*/ | |
function putValue($field){ | |
global $value; | |
if(isset($value[$field])) | |
echo $value[$field]; | |
} | |
function putError($field){ | |
global $error; | |
if(isset($error[$field])) | |
echo "<span class='error'>" . $error[$field] . "</span>"; | |
} | |
if(empty($error) && !empty($value)): | |
echo "<h2>Your Input:</h2>"; | |
foreach ($value as $field => $fieldValue) { | |
echo $field . " is " . $fieldValue; | |
} | |
else: | |
?> | |
<h2>PHP Form Validation Example</h2> | |
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> | |
Name: <input type="text" name="name" value="<?php putValue('name');?>"> | |
<?php putError("name"); ?> | |
<br><br> | |
E-mail: <input type="text" name="email" value="<?php putValue('email');?>"> | |
<?php putError("email"); ?> | |
<br><br> | |
Website: <input type="text" name="website" value="<?php putValue('website');?>"> | |
<?php putError("website"); ?> | |
<br><br> | |
Comment: <textarea name="comment" rows="5" cols="40"><?php echo putValue('comment');?></textarea> | |
<br><br> | |
Gender: | |
<input type="radio" name="gender" <?php if (isset($value['gender']) && $value['gender']=="female") echo "checked";?> value="female">Female | |
<input type="radio" name="gender" <?php if (isset($value['gender']) && $value['gender']=="male") echo "checked";?> value="male">Male | |
<?php putError("gender"); ?> | |
<br><br> | |
<input type="submit" name="submit" value="Submit"> | |
</form> | |
<?php endif; ?> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment