I've used three separate methods to pass form errors.
1. The page with the form would submit data to a php file that, using header("Location: form.php?err=$err"), would return a small string. The form page had a script that checked if $_GET['err'] was set, and if it was, run that through a switch statement. Based on what $err was, it would display an error message. The problem I ran into with this was twofold: I didn't like using GET for errors (like you), and I would forget to add unique messages to my switch statement for each possible error.
2. Then I started using ajax. I love this method because all I have to do is output the result into a <div>. The php page I called with the ajax would do all error checking and output a string if there was an error, or perform an action if there wasn't. Very easy. It was a tad complicated though, and sometimes I lost track of what went where.
3. The best method I've found yet is using sessions. simply put this line wherever you want errors to be displayed:
Code: Select all
<?php
if(!empty($_SESSION['error']))
{
echo $_SESSION['error'];
$_SESSION['error'] = "";
}
?>
Any time your scripts generate an error, simply update $_SESSION['error'] and it will only display once wherever you wanted it to.