User Interaction and Forms - Page 3January 17, 2002 For many applications of PHP, the ability to interact with users who view the Web page is essential. Veterans of JavaScript tend to think in terms of event handlers, which let you react directly to the actions of the user - for example, the movement of the mouse over a link on the page. Server-side scripting languages such as PHP have a more limited scope when it comes to user interaction. As PHP code is activated when a page is requested from the server, user interaction can only occur in a back-and- forth fashion: the user sends requests to the server, and the server replies with dynamically generated pages. The key to creating interactivity with PHP is to understand the techniques we can use to send information about a users' interaction along with their request for a new Web page. PHP makes this fairly easy, as we'll now see. The simplest method we can use to send information along with a
page request uses the URL query string. If you've ever
seen a URL with a question mark following the filename, you've
witnessed this technique in use. Let's look at an easy example.
Create a regular HTML file called
This is a link to a file called To really understand the results of this, we need to look at
Now, if you use the link in the first file to load this second
file, you'll see that the page says "Welcome to our Web site,
Kevin!" The value of the variable passed in the query string of
the URL was automatically placed into a PHP variable called
You can pass more than one value in the query string if you want
to. Let's look at a slightly more complex version of the same
example. Change the link in the HTML file to read as follows
(this is
This time, we'll pass two variables: As before, we can use the two variable values in our
This is all well and good, but we still have yet to achieve our goal of true user interaction, where the user can actually enter arbitrary information and have it processed by PHP. To continue with our example of a personalized welcome message, we'd like to allow the user to actually type his or her name and have it appear in the message. To allow the user to type in a value, we'll need to use an HTML form. Here's the code (
Don't be alarmed at the slashes that appear in some of these tags
(e.g. This form has the exact same effect as the second link we looked
at (with The method attribute of the form tag is used to tell the browser
how to send the variables and their values along with the
request. A value of get (as used above) causes them to be passed
in the query string, but there is an alternative. It's not always
desirable -- or even technically feasible -- to have the values
appear in the query string. What if we included a
This form is functionally identical to the previous one. The only difference is that the URL of the page that's loaded when the user clicks the "GO" button will not have a query string. On the one hand, this lets you include large values, or sensitive values (like passwords) in the data that's submitted by the form, without their appearing in the query string. On the other hand, if the user bookmarks the page that results from their submission of the form, that bookmark will be useless, as it doesn't contain the submitted values. This, incidentally, is the main reason that search engines like AltaVista use the query string to submit search terms. If you bookmark a search results page on AltaVista, you can use that bookmark to perform the same search again later, because the search terms are contained in the URL. That covers the basics of using forms to produce rudimentary user interaction with PHP. We'll cover more advanced issues and techniques in later examples. |