Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > HTML/XHTML

HTML Forms and PHP

  • Print
  • + Share This
This chapter is from the book

Receiving Data from a Form in PHP

Now that you've created a basic HTML form, you need to write the handle_form.php script that will receive and process the data generated by the feedback.html page. For this example, the PHP script will simply repeat what the user entered into the form. In later chapters, you'll learn how to take this information and store it in a database or send it in an email.

For many readers, this example will be surprisingly simple, and the script will work flawlessly. But some may encounter a small hiccup or two, thanks to major changes in PHP's default configuration. In many PHP installations, you can access form values by taking the name attribute of the various inputs and adding a dollar sign to create the variable. In this example, those variables would be $title, $name, $email, $response, $comments, and $submit.

You'll write the script using this standard method, which will work for many readers. If it doesn't, then this leads nicely into the debugging techniques covered next. So, as a heads up, if you see unexpected results with this example, fear not: The next three sections of the chapter explain everything and teach some valuable lessons in the process.

To create the PHP script:

  1. Open your text editor and create a new document (Script 3.4):
    <!DOCTYPE html PUBLIC" -//W3C//
    → DTD XHTML1.0Transitional//EN"
    → xhtml1-transitional.dtd">
    <html xmlns="
    → xhtml">
       <meta http-equiv="content-type"
       →content="text/html; charset=
       →iso-8859-1" />

    Script 3.4. By taking the value of the name elements in the HTML form and adding a dollar sign, you create variables that contain the values the user entered in the corresponding form fields.

    1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
        1.0 Transitional//EN"
    2   "
    3   <html xmlns="">
    4   <head>
    5        <meta http-equiv="content-type"
             content="text/html; charset=
             iso-8859-1" />
    6        <title>Your Feedback</title>
    7   </head>
    8   <body>
    9  <?php // Script 3.4 - handle_form.php
    11 // This page receives the data from
    12  // It will receive: title, name, email,
        response, comments, and submit.
    14  print "Thank you $title $name for your
        comments. <br />";
    15  print "You stated that you found this
        example to be $response and added:
    16  ?>
    17   </body>
    18   </html>
  2. Insert the opening PHP tag and add any comments:

    <?php // Script3.4-handle_form.php
    // This page receives the data from
    // It will receive: title, name, email,
    →response, comments, and submit.

    You add comments to the script to remind yourself of its purpose. Even though the feedback.html page indicates where the data is sent (via the action attribute), a comment here indicates the reverse (from where this script is getting its data).

  3. Print out the user information:

    print "Thank you $title $name for your
    → comments.<br/>";
    print "You stated that you found this
    → example to be $response and added:
    → $comments";

    Again, to access what the user typed into the Name input box, you refer to the $name variable. The same applies to all the other form inputs, regardless of their type.

  4. Close the PHP section and complete the HTML page.
  5. Save the script as handle_form.php.
  6. Upload the script to the server (or store it in the proper directory on your computer if you've installed PHP), making sure it's saved in the same directory as feedback.html.
  7. Test the script in your Web browser by going to feedback.html and then submitting the form (Figures 3.8 and 3.9).

    Figure 3.8

    Figure 3.8 Whatever the user enters into the HTML form should be printed out to the Web browser by the handle_form.php script (see Figure 3.9).

    Figure 3.9

    Figure 3.9 This is another application of the print statement discussed in Chapter 1, but it constitutes your first dynamically generated Web page.

    If the values of the variables aren't printed when you run the script (Figure 3.10), or they're not printed and you see many error messages, these are the aforementioned hiccups. Some of you may see nothing at all (a blank page). In any of these cases, continue reading the next three sections of the book for the appropriate solutions.

    Figure 3.10

    Figure 3.10 If the page doesn't display the submitted information, read the The Register Globals Problem section of the chapter.

  • + Share This
  • 🔖 Save To Your Account