In my last article, I talked about using JavaScript to do integrated form validation, which is sufficient for client validation, but good form validation should be done both on the client and server side-that's what this article is about.
What should you write if you use a generic validation method?
if ($_post[' some '] does not meet the condition) {
Die ("wrong");
}
An integrated authentication method similar to JavaScript, the array and eval statements can also be used in PHP to implement the form validation (server-side) framework. General steps:
1, the user submitted data from the $_get or $_post array to the custom array (optional),
2, fill the validation array (including variable name, conditions and hints, etc.),
3, add a fixed validation code.
Let me give you an example, if you want to collect the user name and age from the Web page and implement data validation on the server side, then you may need form.html and act.php. The form.html contains the form:
<form Method=post action=act.php>
Name: <input type=text name= ' name ' ><br>
Age: <inupt type=text name= ' uage ' ><br>
<input type=submit value= "Submit" >
</form>
Validation of forms is implemented in act.php:
$userinfo = Array (); Step 1
$userinfo [' name '] = isset ($_post[' uname '])? $_post[' uname ']: ';
$userinfo [' age '] = isset ($_post[' uage '])? (int) $_post[' uage ']: 0;
$error _message = ""; Error message
Step 2
$elems = Array (
Array ("\ $userinfo [' name ']", ' strlen (#) <1 ', ' name cannot be empty! '),
If you omit step 1, rewrite as follows:
Array ("\$_post[' uname ']", ' strlen (#) <1 ', ' name cannot be empty! '),
Array ("\ $userinfo [' Age ']", ' #<=0 ', ' ages incorrect! ')
);
Step 3
for ($i = 0; $i < count ($elems); $i + +) {
if (eval ("Preg_replace" ("/[#]/", $elems [$i][0], $elems [$i][1]) return false; ") = = False) {
$error _message. = $elems [$i][2];
}
}
This allows us to complete the form validation each time we just have to populate the validation array $elems.
I hope it works for you, and I'd like to know how you do it.
This allows us to complete the form validation each time we just have to populate the validation array $elems.
I hope it works for you, and I'd like to know how you do it.
This allows us to complete the form validation each time we just have to populate the validation array $elems.
I hope it works for you, and I'd like to know how you do it.