Help - Search - Members - Calendar
Full Version: display multiple errors
Pixel2Life Forum > Help Section > PHP, ASP, MySQL, JavaScript and other Web/Database Programming Help
derek.sullivan
Why wont my code show all the $errors[] at the same time like I want it too?

CODE
<?php

session_start();

echo '<div id="form">'."\n";

if (isset($_POST['submit'])) {

    $correct_password = "";
    
    $admin = "";

    if (empty($_POST['username'])) {
    
        $errors[] = "You did not enter your username.<br />";
        
    }
    if (empty($_POST['password'])) {
    
        $errors[] = "You did not enter your password.<br />";
        
    }
    if ($_POST['username'] !== $admin) {
    
        $errors[] = "This username does not exist.<br />";
    
    }
    if ($_POST['password'] !== $correct_password) {
    
        $errors[] = "You have entered an incorrect password.<br />";
    
    }

    if (count($errors) == 0) {
    
        $_SESSION['admin'] = $_POST['username'];
        echo '<script>location.href="admin.php";</script>';
    
    }else{
        
        echo('<div id="errors">'.$errors[0].' <a href="login.php">Return</a></div>');
    
    }

}
    
echo '    <form action="login.php" method="post">
<label>Username:</label> <input type="username" name="username" class="textbox" /><br />
<label>Password:</label> <input type="password" name="password" class="textbox" /><br />
<div class="buttons"><input type="submit" name="submit" value="Login" /></div>
</form>
</div>'."\n";


?>
Da DreadLord
$errors: should not be an array really (just a string)

then the errors should be added like (notice the . before the =)
CODE
$errors .= "You have entered an incorrect password.<br />";


and
CODE
if (count($errors) == 0)

should be
CODE
if (strlen($errors) > 0)

and
CODE
echo('<div id="errors">'.$errors.' <a href="login.php">Return</a></div>');



smile.gif
Mr. Matt
What Da DreadLord said isnt exactly true, you can use arrays and you can use strings, using arrays is just a prefered method over strings as it gives you so much more control over handling the collection and displaying of errors.

Now to what is wrong:

CODE
echo('<div id="errors">'.$errors[0].' <a href="login.php">Return</a></div>');


All your doing here is echoing the first element of the array, when you want to be showing all of them, and it is a very simple change:

CODE
echo('<div id="errors">'.implode( "\n", $errors ).' <a href="login.php">Return</a></div>');


Im not going to go into the full workings of arrays, you can do that yourself here. The implode function simply joins all of the elements with the first parameter in the array passed as the second paramete rof the array together.

And you will also need to remove the new line characters when you do your error checking.

Hope this helps
derek.sullivan
Thanks for the replies. I'll see if that works now smile.gif

BTW, DreadLord, why would I want to check the string length of the errors? I want to count if there are multiple errors?
rc69
QUOTE (derek.sullivan @ Jun 16 2009, 08:33 PM) *
BTW, DreadLord, why would I want to check the string length of the errors? I want to count if there are multiple errors?

I second Matt's solution, but in answer to your question, i believe you are misunderstanding the problem.

count() count's the elements of an array. strlen() returns the length of a string.
If you are using an array to contain individual errors, you would use count() to see how many errors you have.
If you are concatenating errors onto a string as they occur, you can't count the individual errors, so you must check to see if you have an error at all (strlen() will be > 0 if that is the case).
Da DreadLord
QUOTE (rc69 @ Jun 17 2009, 05:35 AM) *
QUOTE (derek.sullivan @ Jun 16 2009, 08:33 PM) *
BTW, DreadLord, why would I want to check the string length of the errors? I want to count if there are multiple errors?

I second Matt's solution, but in answer to your question, i believe you are misunderstanding the problem.

count() count's the elements of an array. strlen() returns the length of a string.
If you are using an array to contain individual errors, you would use count() to see how many errors you have.
If you are concatenating errors onto a string as they occur, you can't count the individual errors, so you must check to see if you have an error at all (strlen() will be > 0 if that is the case).


what he said tongue.gif

and as for arrays or strings, i looked at the way he displayed the errors (in a div) and how he added his errors (with <br> at the end) and just thought it would be easiest if he changed to string

arrays work too of course

smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.