Page 268 - Beginning PHP 5.3
P. 268

Part III: Using PHP in Practice
                        Capturing Form Data with  PHP

                   You now know how to create an HTML form, and how data in a form is sent to the server. How do you
                 write a PHP script to handle that data when it arrives at the server?
                   First of all, the form ’ s   action  attribute needs to contain the URL of the PHP script that will handle the
                form. For example:
                      < form action=”form_handler.php” method=”post” >

                   Next, of course, you need to create the  form_handler.php  script. When users send their forms, their
                 data is sent to the server and the   form_handler.php  script is run. The script then needs to read the
                 form data and act on it.
                   To read the data from a form, you use a few superglobal variables. You were introduced briefly to
                 superglobals in Chapter  7 . A superglobal is a built - in PHP variable that is available in any scope: at the
                 top level of your script, within a function, or within a class method. Chapter  7  discussed the   $GLOBALS
                superglobal array, which contains a list of all global variables used in your applications. Here, you learn
                about three new superglobal arrays:


                        Superglobal Array        Description

                         $_GET              Contains a list of all the field names and values sent by a form using
                                        the get method
                         $_POST             Contains a list of all the field names and values sent by a form using
                                        the post method
                         $_REQUEST          Contains the values of both the $_GET and $_POST arrays combined,
                                        along with the values of the $_COOKIE superglobal array

                      You learn about the   $_COOKIE  superglobal in the next chapter.

                  Each of these three superglobal arrays contains the field names from the sent form as array keys, with
                the field values themselves as array values. For example, say you created a form using the   get  method,
                and that form contained the following control:


                      < input type=”text ”  name=”emailAddress” value=”” / >
                   You could then access the value that the user entered into that form field using either the  $_GET  or the
                  $_REQUEST  superglobal:
                    $email = $_GET[“emailAddress”];



                    $email = $_REQUEST[“emailAddress”];





              230





                                                                                                      9/21/09   7:23:35 PM
          c09.indd   230                                                                              9/21/09   7:23:35 PM
          c09.indd   230
   263   264   265   266   267   268   269   270   271   272   273