RE: [PHP] How to make a form retain its info
| From: | Boget, Chris | Date: | Thu, 27 Jul 2000 18:42:27 +0000 |
| Subject: | RE: [PHP] How to make a form retain its info | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-8633@lists.php.net to get a copy of this message | ||
> An even better way is to output a new form on the second page
> where all the data is already filled in (using the variables that
> was sent) And maybe a little red star or something to point out
> what was filled in incorrectly.
> So that they can change it and submit it again..
Except that why should a subsequent page have to know anything
about the preceeding page? The answer is that it shouldn't and in
doing this, you are just doubling the amount of code you have (the
initial code on the first page and then the same code on the second
page in case the use has an error).
I like to make all form pages as modular as possible and as such,
they only know about and process their own form elements. When
the form is submitted, it comes back to itself (form action=$PHP_SELF)
to validate the element values and insert/update the database as
needed. If everything is all good, the user is directed to the next
page (along with any necessary variables as GET) otherwise the
code drops down to redisplay the form along with the values that
were entered by the user. On a slight tangent, Jay Bloodworth's
FormClass is an invaluable tool when working with Forms and is
highly useful in a situation like this as it has a mechanic built in
to validate your form elements so you don't have to.
Bearing the above in mind, 99% of all my form pages look some-
thing like this:
------------
// I define all my form elements here using JB's FormClass
include( "form_definition.data" )
if( isset( $submit_button )) {
ok = validateFormVariables
if( ok ) {
update_database_query
ok = mysql( update_database_query )
if( ok ) {
header( "location: page2.php3?var1=$var1&var2=$var2" );
exit();
}
}
print error message here - error from form validate or error from
db query
}
print form out here
----------
That way, each page only knows about itself and you don't have to
duplicate code all over the place. This has worked well for me for
over 2 years and 10+ major websites, so that's got to say something.
DISCLAIMER: as always, YMMV
Chris