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

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

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

Manually Sending Data to a Page

The last example for this chapter is a slight tangent to the other topics but plays off the idea of handling form data with PHP. As you

learned in the section "Using GET or POST," if a form uses the GET method, the resulting URL is something like
→ handle_form.php?title=Mr.&name=
→ Larry%20Ullman...

The receiving page (here, handle_form.php) is sent a series of name=value pairs, each of which is separated by an ampersand (&). The whole sequence is preceded by a question mark (immediately after the handling script's name). In this example, handle_form.php has a $title variable with a value of Mr., a $name variable with a value of Larry Ullman (the %20 is a URL-encoded version of a space), and so forth.

Using this knowledge, you can send data to a PHP page without the use of the form. A simple script will easily demonstrate this concept, sometimes called the "GET method trick."

To create a simple greeting script:

  1. Create a new document within your text editor (Script 3.8):
    <!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.8. This script will receive a variable and its value in the URL.

    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>Greetings!</title>
    7   </head>
    8   <body>
    9   <? php // Script 3.8 - hello.php
    11  // Adjust display_errors and
        error_reporting, if desired.
    12  ini_set ('display_errors', 1);
    13  error_reporting (E_ALL & ~ E_NOTICE);
    15  // This page should receive a name value
        in the URL.
    17  // Say "Hello".
    18  print "<p>Hello, <b>$name</b>!</p>";
    20  ?>
    21  </body>
    22  </html>
  2. Begin the PHP code:
    <?php // Script3.8-hello.php
  3. Address the error management, if desired:

    ini_set ('display_errors',1);
    error_reporting (E_ALL & ~ E_NOTICE);

    These two lines, which configure how PHP responds to errors, may or may not be necessary but can be helpful.

  4. Print a message using an assumed $name variable:

    print "<p>Hello, <b>$name</b>!</p>";

    The $name variable is sent to the page through the URL. To access it, you can use $name if register_globals is enabled or {$_GET['name']} if it isn't. Again, you would use $_GET (as opposed to $_POST) because the value is coming from a GET method, and the curly braces help avoid parse errors.

  5. Complete the PHP code and the HTML page:
  6. Save the script as hello.php and upload it to your Web server.

If you were to view this script now, it would only display Hello, !, because the $name variable has no value (Figure 3.17). To pass the script a $name value, use the GET method trick.

Figure 3.17

Figure 3.17 If the $name variable isn't assigned a value, the browser prints out this awkward message.

To manually send data to a PHP script:

  1. View the hello.php script in your Web browser by going to the appropriate URL, in this case, (Figure 3.17).
  2. Append to the URL the text ?name=Michael. You can (or, frankly, should) use your own name; just ensure that there are no spaces in your entry.
  3. Press Enter (or Return) to reload the page in your browser with the new URL (Figure 3.18).

    Figure 3.18

    Figure 3.18 By setting the $name variable to Michael or some other name, you create a dynamic page that changes to address the specific user.

    If you don't see the name printed in the browser, then you made a mistake. Check to be sure you included the question mark, which separates the file name from the data itself. Then be sure you used name, because that's what hello.php is expecting as a variable (remember that Name would constitute a different variable).

  4. If desired, change the name value in step 2 and press Enter (or Return) to reload the page again (Figure 3.19).

    Figure 3.19

    Figure 3.19 Setting the value of $name to Jude has the same effect as changing the print() line to read print "<p>Hello, <b>Jude</b>!</p>";.

    Each new name value creates a new dynamically generated greeting.

  • + Share This
  • 🔖 Save To Your Account