PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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
<?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>
 
    <?php 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>
 
    <?php } else { ?>
 
      <form method="post" action="<?= $_SERVER['PHP_SELF'] ?>?action=<?= $wizard->getStepName() ?>">
        <h2><?= $wizard->getStepProperty('title') ?></h2>
 
        <?php if ($wizard->getStepName() == 'userdetails') { ?>
          <table>
            <tr>
              <td>Name:</td>
              <td>
                <input type="text" name="name" value="<?= htmlSpecialChars($wizard->getValue('name')) ?>" />
              </td>
              <td>
                <?php if ($wizard->isError('name')) { ?>
                  <?= $wizard->getError('name') ?>
                <?php } ?>
              </td>
            </tr>
            <tr>
              <td>Email:</td>
              <td>
                <input type="text" name="email" value="<?= htmlSpecialChars($wizard->getValue('email')) ?>" />
              </td>
              <td>
                <?php if ($wizard->isError('email')) { ?>
                  <?= $wizard->getError('email') ?>
                <?php } ?>
              </td>
            </tr>
            <tr>
              <td>Country:</td>
              <td>
                <select name="country">
                  <option value=""></option>
                  <?php foreach ($wizard->countries as $k => $v) { ?>
                    <option value="<?= $k ?>"<?php if ($wizard->getValue('country') == $k) { ?> selected="selected"<?php } ?>>
                      <?= $v ?>
                    </option>
                  <?php } ?>
                </select>
              </td>
              <td>
                <?php if ($wizard->isError('country')) { ?>
                  <?= $wizard->getError('country') ?>
                <?php } ?>
              </td>
            </tr>
          </table>
        <?php } else if ($wizard->getStepName() == 'billingdetails') { ?>
          <table>
            <tr>
              <td>Credit Card Type:</td>
              <td>
                <select name="cc_type">
                  <option value=""></option>
                  <?php foreach ($wizard->ccTypes as $v) { ?>
                    <option value="<?= $v ?>"<?php if ($wizard->getValue('cc_type') == $v) { ?> selected="selected"<?php } ?>>
                      <?= $v ?>
                    </option>
                  <?php } ?>
                </select>
              </td>
              <td>
                <?php if ($wizard->isError('cc_type')) { ?>
                  <?= $wizard->getError('cc_type') ?>
                <?php } ?>
              </td>
            </tr>
            <tr>
              <td>Credit Card Number:</td>
              <td>
                <input type="text" name="cc_number" value="<?= htmlSpecialChars($wizard->getValue('cc_number')) ?>" />
              </td>
              <td>
                <?php if ($wizard->isError('cc_number')) { ?>
                  <?= $wizard->getError('cc_number') ?>
                <?php } ?>
              </td>
            </tr>
          </table>
        <?php } 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>
        <?php } ?>
 
        <p>
          <input type="submit" 
                 name="previous" 
                 value="&lt;&lt; Previous"<?php if ($wizard->isFirstStep()) { ?> disabled="disabled"<?php } ?> />
          <input type="submit" 
                 value="<?= $wizard->isLastStep() ? 'Finish' : 'Next' ?> &gt;&gt;" />
        </p>
      </form>
    <?php } ?>
  </body>
</html>

In This Article


Additional Files