While this *CAN* work, and a lot of people like it, it tends to add a fair amount of "cruft" for not that much benefit, really...
What do you GAIN having this big old switch statement? What data/processing is really really shared in all these steps?
On Wed, May 11, 2005 4:57 pm, dan said:
Hello, all -
I've been researching how to handle forms properly, and I think I figured out a way that might be beneficial for me to use. It is as follows:
(index.php)
session_start(); if (isset($_SESSION['step'])) { switch $_SESSION['step'] { case "1": require('step1.php'); break; case "2": require('step2.php'); break; case "3": require('step3.php'); break;
Simpler:
case "1": case "2": case "3": require "step$step.php"; break;
Also not that it's pretty unlikely that your default will kick in, since somebody would have to intentationally hack $step to be, say, 4 or something to reach that line of code... [more]
// add more case statements here if I need to default: require('step1.php'); break; } } else { $_SESSION['step'] = '1'; require('step1.php'); }
So you might want to start off with:
$step = isset($_SESSION['step']) ? $_SESSION['step'] : 1; Then you could do your switch, and default to step1, thereby having all the same functionality, but with fewer branches in the actual algorithm.
Each stepX.php file would look something similar to this:
(step1.php)
// if submitted, check data for completeness // if complete, set 'step' to 2, to be used as argument to index.php $_SESSION['step'] = '2' // redirect back to index.php, use new value of 'step' to direct header('Location: http://somesite.com/index.php'); // else display form data
Now, this is, really, one of my first experiences with doing forms. I just want to know if I can/should/would anticipate any problems down the road while doing this. I think it would work quite well, but I've only been doing this for a short while.
I did it this way at first, but quicly found that the amount of shared code between steps was so minimal, that it was better to just have each step as a separate form, with filenames that made sense for the information gathered at that stage.
It's also sometimes good to let the user fill in different steps in whatever order they prefer -- depending on the data gathered and what your business goals are.
I've seen a particularly nice implementation of this at CDBaby (which is not real useful to anybody reading this unless you happen to be a musician with a CD you want to sell...) where the steps can be done in any order, but the first five are MUST DO and are flagged as such in RED until you do them, and the last three are OPTIONAL and are in yellow until you do them. Completed steps are changed to green.
Since some of the steps could require a fair amount of background work (writing/editing/fixing-up your Bio, for example, or getting a complete track listing with titles in order) he lets you do them in the order that fits into your life, not in the order he happened to program that morning. Very nice.
Richard -
I was just looking for some sort of confirmity and ease of use. I've been experimenting with some of my own ways to handle form data. There's nothing that I hate more than clutter, so that's why I wanted to break the form apart inside of these smaller files.
Thanks! -dant
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php