Making Wizards in Web Apps
October 19th, 2007There is a multitude of ways to make wizards in your web application. When not using AJAX, here is my favorite method.
First, lets lay down some base requirements:
- We have 3 steps in the wizard
- We have a “Complete” page
- The user is able to navigate to previous steps and not lose data
- Nothing is finalized until the last step is finished.
Now that we know what we want out of this wizard, what functions do we need?
- GetWizardSteps($currentStep)
- DisplayWizardSteps($steps)
- SetWizardData($data)
- GetWizardData()
- SaveWizardData()
And, we’ll need the following pages:
- wizardStep1.php
- wizardStep2.php
- wizardStep3.php
- wizardControl.php
- wizardComplete.php
- wizardLibrary.php
What do these functions actually do?
$steps = GetWizardSteps($currentStep);
This function should take an integer for the current step, and spit out an array of steps. Each element in the array should have a step caption, a link (if step is navigable), and any css you want to use to designate past, current, or future steps.
list($html, $css) = DisplayWizardSteps($steps);
You should then be able to take that array, and send it to this function. This function then takes that array of steps, and for every element, writes out appropriate HTML for it. You should return the HTML and CSS you need to display the list of steps, taking into account their status (past, current, future).
SetWizardData($data);
$data can be any object. For the purposes of the wizard, I recommend making it a simple stdClass. The purpose of this function is to allocate a unique section of the $_SESSION to store all the data from the wizard. ex. $_SESSION[”wizard”] = $data
$data = GetWizardData();
This should look in the same place on the session and retrieve the data stored there. If you’re using stdClass to store the data, then: return $_SESSION[”wizard”]; should be sufficient. Otherwise, you may need to implement custom conversion of a simple associative array to the value object of your choice. End result, is that all of the wizard data stored on the session is retrieved as a single object.
$success = SaveWizardData();
This function calls GetWizardData() and then does whatever it needs, to save the data to the database, or file, or where ever you want to save it to. Return true / false for success / failure.
How do the pages tie these functions together?
wizardStep1.php, wizardStep2.php, wizardStep3.php
For each step, there are several things that must be done.
- Set $currentStep
- Generate the steps: $steps = GetWizardSteps($currentStep);
- Display the steps: list($stepsHTML, $stepsCSS) = DisplayWizardSteps($steps);
- Create a <form method=”post” action=”wizardControl.php”>
- Add a hidden input field for the current step.
- Add all content and the submit button to the form.
wizardControl.php
The wizardControl receives all input to process. The task here is to GetWizardData, switch on the $currentStep, and SetWizardData appropriately. If the last step is submitting, then you validate the data, SaveWizardData, and clear out what’s stored as the current wizard data: SetWizardData(false). On success, redirect to the wizardComplete page; on failure, redirect to the step that failed.
wizardLibrary.php
This is just an include page. It doesn’t display anything, or process anything. It simply contains the functions we described earlier.