I have set up a complete example just copy, paste it and give it a try..
This is a very ease way of doing it but it can be somehow cumbersome if
you have alot of inputdata to validate.
Please notyfie me if you get it to work or dont work
<?php
// return error message in the form
function form_error($name){
global $error;
if(isset($error[$name])){
$var = "
<div class=\"error\">
<ul>$error[$name]</ul>
</div>";
return $var;
}
}
// Make sure that the value from the inputfield from the form are in an array
function clean_post($name){
$clean_name = array();
if(isset($_POST[$name])){
$clean_name[$name] = $_POST[$name];
return $clean_name[$name];
}
}
// ascape dangerous characters in submitted data.
function clean_html($output){
$output = ucfirst($output);
$output = htmlentities($output,ENT_QUOTES, 'ISO-8859-1');
return $output;
}
// When the form is submitted... then action is taken
if(isset($_POST['submit'])){
$first_name = clean_post('first_name');
$last_name = clean_post('last_name');
//formvalidation begins
$error = array();
if(!strlen($first_name)){ // make sure that they have entered a firstname
$error['first_name'] = "<li>You have to enter a firstname.</li>";
} else if(!ctype_alpha($first_name)){ // make sure tha the firstname only consits of letters.
$error['first_name'] = "<li>Your firstname can only consist letters.</li>";
} else if(!strlen($last_name)){ // make sure that they have entered a lastname
$error['last_name'] = "<li>You have to enter a lastnamn.</li>";
} else if(!ctype_alpha($last_name)){ // make sure tha the lastname only consits of letters.
$error['last_name'] = "<li>Your lastname can only consist letters.</li>";
// You can add as many if else statment as you need before your output a message.
} else {
// print out a message on the screen.
$output = "
Thank you " . clean_html($first_name) . " " . clean_html($last_name) . " for submiting the form
";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="sv" xml:lang="sv">
<head>
<title>Login</title>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="content-language" content="sv" />
<style type="text/css">
html {
font-size: 125%;
}
body {
font: 50% new-roman, arial, sans-sarif;
}
p{
font-size: 1.4em;
padding: 1em;
}
*{margin: 0;padding:0}
div#center{
width: 40em;
margin: 2em auto;
}
form#login{
background: #f1f1f1;
margin-top: 1em;
-moz-border-radius: 1em;
-moz-box-shadow: 3px 3px 3px #888;
}
form#login fieldset{
padding: 1em;
border: none;
}
form#login label{
float: left;
width: 10em;
font-size: 1.2em;
color: gray;
}
form#login input[type="submit"]{
background: #ddd;
border: none;
color: #333;
display: block;
margin: 0 auto;
width: 5em;
padding: .4em;
}
form#login div.containerInput{
padding: 10px;
margin: 10px;
}
form#login div.error{
background: #BF4747;
padding: 5px;
margin: 10px
}
form#login div.error ul li{
list-style-position: inside;
color: #fff;
font-size: 1.2em;
}
</style>
</head>
<body>
<div id="center">
<p><?php print($output);?></p>
<form action="<?php echo $_SERVER['SCRIPT_NAM'];?>" method="post" id="login">
<fieldset>
<div class="containerInput">
<label for="first_name">Firstname: </label>
<input id="first_name" type="text" name="first_name" value="<?php echo clean_html($first_name);?>" />
</div>
<?php echo form_error('first_name');?>
<div class="containerInput">
<label for="last_name">Lastname: </label>
<input id="last_name" type="text" name="last_name" value="<?php echo clean_html($last_name);?>" />
</div>
<?php echo form_error('last_name');?>
<input type="submit" name="submit" value="Submit"/>
</fieldset>
</form>
</div>
</body>
</html>