Re: Another sessions question
| From: | Justin French | Date: | Wed, 30 Apr 2003 13:59:46 +0000 |
| Subject: | Re: Another sessions question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-145785@lists.php.net to get a copy of this message | ||
So, instead of:
<?
if($error) { echo $error['whatever']; }
?>
... shouldn't it be something like ...
<?
if(count($error) > 0)
{
echo count($error)." errors were found in your form entry:<br>\n";
foreach($error as $key => $val)
{
echo "- {$error[$key]}<br>\n";
}
}
?>
Given the array:
$error['first'] = 'you did not enter your first name';
$error['last'] = 'you did not enter your last name';
$error['email'] = 'you did not enter your email address';
Using your code would echo:
Array()
Using my code would echo:
<b>3 Errors were found in your form entry</b><br>
- you did not enter your first name<br>
- you did not enter your last name<br>
- you did not enter your email address<br>
Is that what you're after?
Justin
on 01/05/03 11:49 PM, C.R. (odcs@rogers.com) wrote:
> To be more precise of the problem - I have a form with roughly 20 fields in
> total over 5 pages (i.e. name, address, email, zip, phone, etc.). if the
> user fills out these fields incorrectly he would get a message for each
> (i.e. Not a valid zipcode, Not a valid phone number, etc.) these are stored
> in a session as $error['zip'] = "Not a valid zipcode",
> $error['phone'] =
> "Not a valid phone number", etc. Each time before the validation of the form
> the $error array is emptied with $error = Array() (it could be unset, but
> would make no difference to my problem.). If the user were to make further
> errors, he would then get lets say 'Not a valid email', which is stored in
> $error['email'], and so on. I got the idea of doing it this way from a PHP
> tutorial (I think it was an O'Reily book if I'm not mistaken) and seemed to
> be the easiest way. To carry the error messages in the url I don't think
> would be a good way to do this, and in any event I already have the session
> started to hold the input of the user until the end of the form so that it
> can be written to the database - so as a session is already being used, I
> might as well use it for the errors as well.
>
> One other note, $_SESSION['error'] is a pain - then I would need
> $_SESSION['error']['zip'], etc. - been there.
>
> TIA
>
> ----- Original Message -----
> From: "Justin French" <justin@indent.com.au>
> To: "C.R." <odcs@rogers.com>; "PHP General"
> <php-general@lists.php.net>
> Sent: Wednesday, April 30, 2003 12:53 AM
> Subject: Re: [PHP] Another sessions question
>
>
>> 1. Perhaps you are confusing the uses of arrays and vars?
>>
>> Are you wanting to do this:
>> $error = 'your password was not correct'; //good
>>
>> Or this:
>> $error['your password was not correct'] = '';
>> // bad way to do this
>>
>> Or this:
>> $error['login'] = 'your password was not correct'
>> // good
>>
>> Or this:
>> $error[] = 'your password was not correct'
>> // good way to queue up multiple errors
>>
>>
>> 2. Clearing the array with $error = Array() doesn't clear the array, it
> just
>> empties it, which is why you're getting "Array" on the page. You should
> be
>> clearing the array with
>>
>> unset($error);
>>
>>
>> 3. In regards to sessions, I don't think you NEED to be using sessions for
>> the errors... they could be passed around in the URL.
>>
>>
>> 4. Your code will be a lot cleaner, more secure, and debugging will be a
>> little bit easier if you refer to variables with their superglobal arrays
>>
>> $_SESSION['error'] instead of $error;
>> $_GET['error'] instead of $error;
>>
>> etc etc.
>>
>> Similarly
>> $_SESSION['error'] = array('foo','bar');
>> $_SESSION['error'] = 'you did not enter a username';
>> unset($_SESSION['error']);
>>
>> etc etc.
>>
>>
>> Justin
>>
>>
>> on 01/05/03 3:54 PM, C.R. (odcs@rogers.com) wrote:
>>
>>> I have a login, registration, update, and a form if you forget your
>>> password. I am using sessions to control the login and error messages.
> So
>>> this is my problem
>>>
>>> When a user enters the login page a session is started, if the user
> enters
>>> the wrong login info they will get an error message which is set in a
>>> session variable called $error['whatever'] (this will change depending
> on
>>> the error). If the user then decides he should register first, and goes
> to
>>> the register page - he gets a message on the page that simply says
> 'Array'.
>>> This is because the session is still active and $error['whatever'] is
> still
>>> set, although I've cleared the array with - $error = array(); On each
> page
>>> I have something like if($error) { echo $error['whatever']; }. I have
> used
>>> done it this way to make things more uniform and understandable, but I
> may
>>> also have shot myself in the foot - is there a way to get around this
>>> without having to change the session variables for each of these pages.
>>>
>>> I hope I have explained this properly, or for that matter used sessions
>>> properly - I am still trying to learn about sessions and problems like
> this
>>> keep popping up.
>>>
>>> TIA
>>>
>>>
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
>