Page 288 - Beginning PHP 5.3
P. 288

Part III: Using PHP in Practice

              Try It Out     Create a Multi-Step Form
                You can use hidden fields to create a series of forms that guide the user through the data entry process
                step by step. Within each form, you can store the current step — so that the script knows what stage
                the user has reached — as well as the data already entered by the user in other steps.
                Here’s an example that splits the previous registration.php form into three steps:

                   ❑   First name/last name
                   ❑   Gender/favorite widget
                   ❑   Newsletter preference/comments

                Save the following script as registration_multistep.php in your document root folder and run
                the script in your Web browser. Try filling in some field values and using the Back and Next buttons to
                jump between the three steps. Notice how the field values are preserved when you return to a
                previously completed step. Figure 9-8 shows the first step of the form, and Figure 9-9 shows the
                second step.
                    To keep things simple, this script doesn’t validate any form fields in the way that registration.php
                    does. However, you could easily use the same techniques used in registration.php to validate each
                    step of the form as it is submitted.

                    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                      “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
                    <html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
                      <head>
                        <title>Membership Form</title>
                        <link rel=”stylesheet” type=”text/css” href=”common.css” />
                      </head>
                      <body>

                    <?php

                    if ( isset( $_POST[“step”] ) and $_POST[“step”] >= 1 and $_POST[“step”]
                    <= 3 ) {
                      call_user_func( “processStep” . (int)$_POST[“step”] );
                    } else {
                      displayStep1();
                    }

                    function setValue( $fieldName ) {
                      if ( isset( $_POST[$fieldName] ) ) {
                        echo $_POST[$fieldName];
                      }
                    }









              250





                                                                                                      9/21/09   7:23:44 PM
          c09.indd   250                                                                              9/21/09   7:23:44 PM
          c09.indd   250
   283   284   285   286   287   288   289   290   291   292   293