Creating Multi-Step Forms And Wizards In PHP
A Full Example Using Our Checkoutwizard Class
Now that we’ve created our wizard class, we must put it to use. Here is a complete script that uses the wizard. This includes instantiating the class, generating all the forms, processing the data and outputting error messages.
Additionally, there is also output once the wizard is complete that gives the option to reset the form and start again.
Listing 8 listing-8.php
require_once('CheckoutWizard.class.php'); $wizard = new CheckoutWizard(); $action = $wizard->coalesce($_GET['action']); $wizard->process($action, $_POST, $_SERVER['REQUEST_METHOD'] == 'POST'); // only processes the form if it was posted. this way, we // can allow people to refresh the page without resubmitting // form data <html> <head> <title>phpRiot() wizard example</title> </head> <body> <h1>phpRiot() wizard example</h1> if ($wizard->isComplete()) { <p> The form is now complete. Clicking the button below will clear the container and start again. </p> <form method="post" action=" $_SERVER['PHP_SELF'] ?action= $wizard->resetAction "> <input type="submit" value="Start again" /> </form> } else { <form method="post" action=" $_SERVER['PHP_SELF'] ?action= $wizard->getStepName() "> <h2> $wizard->getStepProperty('title') </h2> if ($wizard->getStepName() == 'userdetails') { <table> <tr> <td>Name:</td> <td> <input type="text" name="name" value=" htmlSpecialChars($wizard->getValue('name')) " /> </td> <td> if ($wizard->isError('name')) { $wizard->getError('name') } </td> </tr> <tr> <td>Email:</td> <td> <input type="text" name="email" value=" htmlSpecialChars($wizard->getValue('email')) " /> </td> <td> if ($wizard->isError('email')) { $wizard->getError('email') } </td> </tr> <tr> <td>Country:</td> <td> <select name="country"> <option value=""></option> foreach ($wizard->countries as $k => $v) { <option value=" $k " if ($wizard->getValue('country') == $k) { selected="selected" } > $v </option> } </select> </td> <td> if ($wizard->isError('country')) { $wizard->getError('country') } </td> </tr> </table> } else if ($wizard->getStepName() == 'billingdetails') { <table> <tr> <td>Credit Card Type:</td> <td> <select name="cc_type"> <option value=""></option> foreach ($wizard->ccTypes as $v) { <option value=" $v " if ($wizard->getValue('cc_type') == $v) { selected="selected" } > $v </option> } </select> </td> <td> if ($wizard->isError('cc_type')) { $wizard->getError('cc_type') } </td> </tr> <tr> <td>Credit Card Number:</td> <td> <input type="text" name="cc_number" value=" htmlSpecialChars($wizard->getValue('cc_number')) " /> </td> <td> if ($wizard->isError('cc_number')) { $wizard->getError('cc_number') } </td> </tr> </table> } else if ($wizard->getStepName() == 'confirm') { <p> Please verify the entered details and then click next to complete your order. </p> <table> <tr> <td>Name:</td> <td> $wizard->getValue('name') </td> </tr> <tr> <td>Email:</td> <td> $wizard->getValue('email') </td> </tr> <tr> <td>Credit Card Type:</td> <td> $wizard->getValue('cc_type') </td> </tr> <tr> <td>Credit Card Number:</td> <td> $wizard->getValue('cc_number') </td> </tr> </table> } <p> <input type="submit" name="previous" value="<< Previous" if ($wizard->isFirstStep()) { disabled="disabled" } /> <input type="submit" value=" $wizard->isLastStep() ? 'Finish' : 'Next' >>" /> </p> </form> } </body> </html>




