Sending Data without a Form

Sending Data without a Form

Sometimes it can be very handy to send data to a server-side program without necessarily using a form. This is a little-known trick that can really enhance your Web pages without requiring a lick of PHP programming. The Link Demo page (linkDemo.html) shown in Figures 2.9 and 2.10 illustrate this phenomenon.

Click To expand
Figure 2.9: The links on this page appear ordinary, but they are unusually powerful.
Click To expand
Figure 2.10: When I clicked on the "Hi Elizabeth" link, I was taken to the HiUser program with the value "Elizabeth" automatically sent to the program!

Understanding the get Method

All the links in the linkDemo.html page use a similar trick. As you recall from earlier in the chapter, form data can be sent to a program through two different methods. The post method is the technique you'll usually use in your forms, but you've actually been using the get method all along, because normal HTML requests actually are get requests. The interesting thing about that is that you can send form data to any program that knows how to read get requests by embedding the request in your URL. As an experiment, switch the method attribute of whatsName.html so the form looks like this:

<form method = "get"
      action = "hiUser.php">

Then run the page again. It will work the same as before, but the URL of the resulting page will look like this (presuming you said the user's name is "Andy"):

The get method stashes all the form information into the URL using a special code. If you go back to the whatsName page and put in "Andy Harris," you'll get a slightly different result:

The space between "Andy" and "Harris" was converted to a plus sign because space characters cause a lot of confusion. When form data is transmitted, it often undergoes a number of similar transformations. In PHP programming, all the translation is automatic, so you don't have to worry about it.

Using a URL to Embed Form Data

If you understand how this works, you can use a similar technique to harness any server-side program on the Internet. (Presuming it's set up to take get-method data—some are not.) When I examined the URLs of Google searches, I could see my search data in a field named "q" (for query, I suppose). I took a gamble that all the other fields would have default values, and wrote a hyperlink that incorporates a query. My link looked like this:

<li><a href = "">
      Google search for "php"</a></li>

Whenever the user clicks on this link, it sets up a get-method query to google's search program. The result is a nifty Google search. One fun thing you might want to do is figure out how to set up "canned" versions of your most common queries in various search engines so you can get updated results with one click. Figure 2.11 illustrates what happens when the user clicks on the "google php" link in the linkDemo page.

Click To expand
Figure 2.11: The Google PHP runs a search on for the term "PHP".

Figure 2.12 shows the results of this slightly more complex search.

Click To expand
Figure 2.12: The Google search for "Absolute Beginners Programming" shows some really intriguing book offerings!
<li><a href =
      " for the absolute beginner">
       Google search for "programming absolute beginner"</a></li>


There's a down side to this approach. The owner of the program can change the program without telling you, and your link will no longer work correctly. Most Web programmers assume that their programs will be called only by the forms that they originally built.

The other thing to consider is people can do this with your programs. Just because you intend for your program to be called only by a form doesn't mean that's how it will always work. Such is the vibrant nature of the free-form Internet.

Working with Multiple Field Queries

As one more practical example, the code for the National Weather service link looks like this:

<li><a href =     "
National Weather Service Forecast</a>
for Noblesville, Indiana.

While this link looks a little more complex, it didn't require any special knowledge. I simply searched the National Weather Service Web site until I found the automatically generated page for my hometown. When I looked at the URL that resulted, I was pleased (but not surprised) to see that the page was generated by a PHP script. (Note the .php extension in the URL.) I copied the link from my browser and incorporated it into linkDemo.html. The weather page is automatically created by a PHP program based on two inputs (the county and city name). Any time I want to see the local weather, I can re-call the same query even though the request doesn't come directly from the National Weather Service. This is a really easy way to customize your Web page.

In the last paragraph I mentioned that the PHP program requires two fields. I've never actually seen the program, but I know this because I looked carefully at the URL. The part that says warncounty=INCO57 indicates the state and county (at least that's a reasonable guess), and the city=Noblesville indicates which city within the county I'm interested in. When a form has two or more input elements, they are attached to each other with the ampersand (&) as you can see in the National Weather Service example.