On Wed, 20 Mar 2002, David Johansen wrote:
> I was just wondering what was the best way to handle a multipage form. Would
> the best way be to keep passing the variables through the forms as hidden
> values or should I use sessions and store all the values as session
> variables? What's the best way to handle all of this? Also the different
> forms will all be in the same php file that loops to itself if that makes a
> difference.

Erik gives you some good advice.

The best I can add is to minimize your expectations about rational 
behavior on the part of your users. People will use the back button to go 
back and change their answers, etc., no matter how much you tell them not 
to.

For really complicated forms I usually build (from leftover parts in our
virtual basement) a system something like this:

1) An array ('form_fields') describing all the form items on all the
pages. This describes what type of form element, how it's labeled, which
other elements it depends on, whether it's mandatory, etc.

2) Another array ('field_pages') that says which of these should ideally
appear on which apges, and in which sequence.

3) A set of functions that can draw any form element based on the 
definitions in array form_fields above.

4) Iterate through array field_pages above, drawing all the items on the
page we currently think we're on (this is the one thing we store in a
hidden variable; everything else stays in a session array).

5) When the form is submitted, check through the items that array
field_pages says should be on the page identified by the hidden variable.
If any marked as mandatory are not completed, generate an error label 
indexed on its form_fields key. Do the same for any other errors.

6) If we have anything in our error label array, re-draw those fields, 
labeled with the appropriate error messages, and return to step 5.

7) Otherwise, increment our index into field_pages and draw some more
(returning to step 4), until there's nothing left.

It's a bit of work to set up the first time, but well worth it, because 
your complex forms will be more robust than most.

miguel


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to