Jump to content


display multiple errors


5 replies to this topic

#1 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 16 June 2009 - 02:27 PM

Why wont my code show all the $errors[] at the same time like I want it too?

<?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";


?>

Edited by derek.sullivan, 16 June 2009 - 02:29 PM.


#2 Da DreadLord

    Young Padawan

  • Members
  • Pip
  • 14 posts
  • Gender:Male
  • Location:Aalst, Belgium

Posted 16 June 2009 - 03:56 PM

$errors: should not be an array really (just a string)

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

and
if (count($errors) == 0)
should be
if (strlen($errors) > 0)
and
echo('<div id="errors">'.$errors.' <a href="login.php">Return</a></div>');


;)

Edited by Da DreadLord, 16 June 2009 - 03:58 PM.


#3 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 16 June 2009 - 05:00 PM

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:

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:

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

Edited by Mr. Matt, 16 June 2009 - 05:02 PM.


#4 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 16 June 2009 - 09:33 PM

Thanks for the replies. I'll see if that works now ;)

BTW, DreadLord, why would I want to check the string length of the errors? I want to count if there are multiple errors?

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 16 June 2009 - 10:35 PM

View Postderek.sullivan, on Jun 16 2009, 08:33 PM, said:

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).

#6 Da DreadLord

    Young Padawan

  • Members
  • Pip
  • 14 posts
  • Gender:Male
  • Location:Aalst, Belgium

Posted 17 June 2009 - 02:22 AM

View Postrc69, on Jun 17 2009, 05:35 AM, said:

View Postderek.sullivan, on Jun 16 2009, 08:33 PM, said:

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 ;)

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

:flowers:





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users