PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.10 USD)

More information
Related Books
PHP and MySQL Web Development (4th Edition) (Developer's Library)

PHP and MySQL Web Development (4th Edition) (Developer's Library)

PHP and MySQL are popular open-source technologies that are ideal for quickly developing...

PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide

PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide

It hasn't taken Web developers long to discover that when it comes to creating dynamic,...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

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


Tagged in

Additional Files