Web EndMemo

PHP Form Validation


PHP can be used for form validation. In following example, there are 3 field, name, password, e-mail which all need to be filled to submit for processing. If not, print "form submit failed" on the screen.

: Name

: Password

: E-Mail



<FORM name=tt action="<?PHP $_SERVER['PHP_SELF']?>" method="post">
<input name=n value='<?php echo "$n"; ?>' style="font-size:16px;
   height:26px;width:250px">: Name<br><br>
<input name=p value='<?php echo "$p"; ?>' type=password style="
   font-size:16px;height:26px;width:250px">: Password<br><br>
<input name=e value='<?php echo "$e"; ?>' style="font-size:16px;
   height:26px;width:250px">: E-Mail<br><br>
<input type="submit" style="height:26px;width:80px" 
   name="submit" value="Submit" />
</FORM>
<?PHP
if ($flag == 0 && isset($_POST['submit'])) echo "<br><br><font color=\"red\">form submit failed.
   </font>\n";
else if ($flag == 1 && isset($_POST['submit'])) echo "<br><br><font color=\"red\">form submit success.
   </font>\n";
?>

The PHP validation code in the beginning of the file:
<?PHP
$flag =0;
$n="";
$p="";
$e="";

if (isset($_POST['submit']))
{
	if (isset($_POST['n'])) $n = $_POST['n'];
	if (isset($_POST['p'])) $p = $_POST['p'];
	if (isset($_POST['e'])) $e = $_POST['e'];
	if ($n=="" || $p=="" || $e=="") $flag=1;
}
?>