
Originally Posted by
pazza86
thanks for that. I'm away on work at the moment but would I be right to assume that as I have multiple fields in the form I would just continue on using the elseif statement?
I wouldn't. If you want to print all the errors just use a foreach loop
PHP Code:
foreach($errors as $error) {
echo $error;
}
I use a foreach loop in my messages function which prints out errors, warnings, success messages, etc., and then just put that in my page in my <header> and then process all PHP before the header, so that if something goes wrong it will print in the messages function.
Doing it this way ensures that nothing important happens if an error is set, because you're only performing important actions when the $error array is empty. I set the $error array in my config file so it's empty on every page load.
Of course, this is an overly simple example. I check for $error, $success, and $warning in my messages function and print out accordingly. I can also use the key in the error array to pass special messages to my form class so that each field will have a customized message printed next to it. It's pretty flexible doing it this way. 
PHP Code:
$error = array();
function messages() {
global $error;
if(!empty($error)) {
foreach($error as $err) {
$return .= $err;
}
return $return;
} else {
return false;
}
}
if($_POST['foo']) == '' )
$error[] = 'Foo can not be empty';
}
if(empty($error)) {
$db->query("INSERT...");
}
echo messages();
Bookmarks