Page 304 - Beginning PHP 5.3
P. 304

Part III: Using PHP in Practice
                  Summary

                   This chapter has shown you how to deal with Web forms within your PHP scripts. You learned:

                   ❑       How to create Web forms, including all the different types of controls that you can place in
                       a form
                   ❑       All about the   $_GET ,  $_POST , and  $_REQUEST  superglobals, and how your scripts can use them
                       to capture form data sent by a user
                   ❑       Some of the security issues surrounding Web forms, and how to mitigate them
                   ❑       How to deal with empty form fields
                   ❑       How to get the PHP engine to recognize multi - value fields, and how to read the data that these
                       fields send
                   ❑       How to generate Web forms from within your PHP scripts. This allows you to add more
                       interactivity and flexibility to your forms. You worked through an example of creating such an
                       interactive form
                   ❑       The concept of hidden form fields and how to use them to store data between page requests.
                       You used this technique to create a three - stage registration form
                   ❑       How to handle files uploaded via Web forms, including using the   $_FILES  superglobal to read
                       information about uploaded files
                   ❑       How to redirect the browser after a form submission in order to display a thank - you page and
                       avoid issues with page reloads
                   HTML forms are a great way to add interactivity to your Web applications. You can use the skills you ’ ve
                 learned in this chapter to produce a wide variety of interactive Web forms, from contact forms and
                 registration scripts through to login forms, online store checkout forms, and  “ tell - a - friend ”  functions.
                   In the next chapter you look at how to store application data between page requests, which means your
                 PHP applications can have a longer lifetime than just a single page view. Meanwhile, try the following
                 two exercises to test your form - handling knowledge. You can find the solutions to these exercises in
                 Appendix A.



                  Exercises
                      1.       Write a simple number - guessing game in PHP. The script should  “ think ”  of a random number
                       between 1 and 100, then give the user five chances to guess the number. For each guess, the
                       script should report whether the guessed number was too low, too high, or correct. (Hint: Use
                         rand( 1, 100 )  to generate a random number between 1 and 100.)
                      2.       Create a script that displays a form allowing the user to select one of three Amazon  stores  —
                        amazon.com , amazon.ca, and  amazon.co.uk  —  and  then jumps to the relevant store based on









                       the user ’ s choice.
              266



                                                                                                      9/21/09   7:23:51 PM
          c09.indd   266                                                                              9/21/09   7:23:51 PM
          c09.indd   266
   299   300   301   302   303   304   305   306   307   308   309