Re: passing form data using $_SESSION
| From: | Curt Zirzow | Date: | Tue, 21 Oct 2003 03:18:18 +0000 |
| Subject: | Re: passing form data using $_SESSION | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-166877@lists.php.net to get a copy of this message | ||
* Thus wrote Chris W. Parker (cparker@swatgear.com):
> Hiya.
>
> The way I do my forms is I create a form page and a corresponding
> processing page. I don't like to post forms to themself so I always make
> a processing page that header("Location: ...")'s back to the original
> page.
>
> if(the name is blank)
> {
> $errors = "e_name=Name cannot be blank&";
> }
>
> $location = "originalpage.php?$errors";
>
> So then I redirect with the $location variable.
>
> But instead of passing it back through the querystring, what about
> assigning the values to the $_SESSION array and simply checking for the
> existence of those values on the original form?
I usually handle error's within session and go back with the
location by the referer (or a hidden input var) and display the
errors on the form. I usually designate a namespace for errors.
For example:
validate.php
if(field isn't valid) {
$_SESSION['errors'] = array('field', 'The field isnt valid');
}
form.php:
if (!empty($_SESSION['errors'])) {
include('common_error_handler.php');
}
common_error_handler.php:
foreach ($_SESSION['errors'] as $field => $desc) {
echo "$field: $desc<br>";
}
unset($_SESSION['errors']);
I do the same with messages (ie 'Data has been saved'). Cept I
usually designate different html div tags with css classes, so the
colors and stuff can be changed for error's vs. messages.
> I think it will work fine (and even better than posting everything) but
> I'm wondering if there's something I'm not considering that makes this
> Not A Good Idea(tm)?
There are some gotchas and isn't the most elegant way to handle
errors, but It is much better than using a GET var to pass back
errors to the form.
Curt
--
"My PHP key is worn out"
PHP List stats since 1997:
http://zirzow.dyndns.org/html/mlists/