Validating form input in PHP
is it possible to write a PHP page say form.php with a php function
generalValidate($form) that is able to perform the following scenario :
user browse to form.php
user gets an html form
user fills form and the form is sent back with POST method to form.php
form.php activate generalValidate($form) where form is the just recived
form filled by user
generalValidate($form) returns true if this form is valid (for exemple
properly filled), false else.
i think that another way to describe my question will be - is there a
general way to iterate over an HTML form sent by a client (perhaps a form
that the php page itself sent to client in the first place) while
activating some code over each of the form values?
dont eat me if its a bad question, im really just trying to learn :)
a code exemple to fill for your convinience :
<?php
function generalValidate($form) {
...
}
if (!isset($_SESSION))
session_start();
else if (generalValidate(...)) {
}
?>
<html>
<head>
</head>
<div>
<p>Register</p>
</div>
<form id="regfrm" action="<?php echo $_SERVER["PHP_SELF"]; ?>"
method="post" align="center">
<table align="center">
<tr>
<td>First Name :</td>
<td><input name="fname" value="<?php if
(isset($_POST["fname"])) {echo v($_POST["fname"]);}?>"
required></input></td>
</tr>
<tr>
<td>Last Name :</td>
<td><input name="lname" value="<?php if
(isset($_POST["lname"])) {echo v($_POST["lname"]);}?>"
required></input></td>
</tr>
<tr>
<td>Email :</td>
<td><input id="email" name="email" value="<?php if
(isset($_POST["email"])) {echo v($_POST["email"]);} else
{echo "xo@xo.xo";}?>" required></input></td>
</tr>
<tr>
<td>Password :</td>
<td><input name="pw" type="password" value="e"
required></input></td>
</tr>
<tr>
<td>Retype password :</td>
<td><input name="pw" type="password" value="e"
required></input></td>
</tr>
</table>
<input type="submit" value="Register" ></input>
</form>
</body>
</html>
No comments:
Post a Comment