.error{
font-weight:bold;
color:#FF0000;
}
<?php
class error
{
function error
(){
$errors =
array();
}
function setError
($new){
global $errors;
$errors[] =
$new;
}
function showErrors
(){
global $errors;
if($this->
countErrors() >
0){
foreach ($errors as $err){
echo "<div class=\"error\">$err</div>";
}
}
}
function countErrors
(){
global $errors;
return count($errors);
}
}
?>
<?php
require_once "class.error.php";
$e =
new error;
if($_POST){
$name =
$_POST['name'];
if(strlen($name) ==
0){
$e->
setError("Name is not valid");
}
if($e->
countErrors() ==
0){
//Since there aren't any errors process the form
header("location: success.php");
die();
}
}
?>
<div align=
"center"><?php
$e->
showErrors(); ?></div>
<form action=
"<?php echo $_SERVER['PHP_SELF'] ?>" method=
"post">
<table border=
"0" cellspacing=
"0" style=
"margin:0px auto">
<tr>
<td colspan=
"2">Change your name:</td>
</tr>
<tr>
<td>Name:</td>
<td><input name=
"name" type=
"text" value=
"<?php echo $name ?>" /></td>
</tr>
<tr>
<td colspan=
"2" align=
"center"><input name=
"Submit" type=
"submit" value=
"Edit" /></td>
</tr>
</table>
</form>