Passing Form Errors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Passing Form Errors

Post by aspekt9 »

What's the easiest way to pass form validation errors back to the user. I have a simple ajax script that displays errors on the same page above the form after it's been submitted and validated by register.php. Is there another way to do keep the errors on the same page above the form that wouldn't involve ajax? Also, to validated a user's login information I return the errors using $_GET i.e (reg.php?msg=1) then I just have it echo a simple error if it's found.. is this a decent way to do things? I think it look sloppy passing the errors using $_GET. Perhaps someone could point me in the direction of a simple ajax error display script that is simple and clean. I use this one currently w/ mootools:

Code: Select all

    <script type="text/javascript">
        window.addEvent('domready', function(){
                    $('memberForm').addEvent('submit', function(e) {
                        new Event(e).stop();
                        var log = $('dispErrors').empty().addClass('ajax-loading');
                        this.send({
                            update: log,
                            onComplete: function() {
                                log.removeClass('ajax-loading');
                            }
                        });
                    });
                });
    </script>
Thanks!
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Passing Form Errors

Post by Chalks »

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.
Dynamis
Forum Contributor
Posts: 122
Joined: Thu Jul 10, 2008 3:15 pm
Location: Indiana, US

Re: Passing Form Errors

Post by Dynamis »

I use sessions for errors as well, but multiple, not just one as previously stated. So on the page that handles my form, I add all the errors to an array and then set that array to a session variable:

Code: Select all

 
if( something bad 1)
     $errors[] = "This is the first error";
if( something bad 2)
     $errors[] = "This is the second error";
 
As you can see, using $error[] adds the new error to the end of the array. After all the errors are added, pass the array to a session variable:

Code: Select all

 
$_SESSION['errors'] = $errors;
 
Finally on the page that wants to display the errors, simply run through the array and print out all the errors:

Code: Select all

 
$errors = $_SESSION['errors'];
for($x=0; $errors[$x]; $x++){
     echo $errors[$x]."<br>";
}
$_SESSION['errors'] = "";  //remove all errors
 
Post Reply