Web Design / Database Project

Please note:

This material is adapted from Build Your Own Database Driven Website Using PHP and MySQL, by Kevin Yank. 2nd Edition, published by Sitepoint Pty. Ltd. ISBN 0-9579218-1-0. The book includes much more information, and you are encouraged to buy it!

Chapter 3 Notes, page 2

User Interaction and Forms

The ability to interact with users who view your website is essential. PHP, as a server-side scripting language, can interact in a back-and-forth manner: the user sends a request to the server, and the server responds with dynamically created pages. We need to understand how to send information about a user's interaction along with those requests.

The simplest method is to use the URL query string. Look at the source code of the file welcome1.html. The query string is the portion of the URL that follows the question mark.

Note that this page includes a link that loads welcome1.php, and informs the PHP code that name equals "Kevin." Looking at welcome1.php, you see the array variable $_GET, which is created by PHP, and which contains any values that are passed in the query string. Since it is an associative array, the value of the name variable that is passed in the query string can be accessed as $GET_['name']. The script assigns this value to the PHP variable "$name" and also displays in as part of a text string, using the echo function.

You can pass more than one value in the query string. Look at welcome2.html and welcome2.php. The first and second variables are passed using the ampersand; you could pass even more that way.

However, to have real interaction, we need to be able to accept arbitrary information from the user and have the PHP process it and produce a correct result. To allow the user to type in his or her name, we will use an HTML form. Take a look at welcome3.html. The form allows the user to input any name, and it references welcome2.php. However, this time, welcome2.php is getting the variables from user input, instead of a predetermined string.

Using Other Method Attributes

The method attribute of the form tag is use to tell the browser how to send the variables and their values along with the request. A value of get causes these to be passed in the query string, and appear in PHP's $_GET array. However, there are alternatives. It is not always a good idea, or even feasible, to have the values show up in the query string. Consider what would happen if there were a large amount of data in the form - such as someone might enter into a <textarea>. This would be much too long and would exceed the maximum length for a URL.

There are alternative methods. If we use the method post, the browser will pass the information invisibly. Look at welcome4.html and welcome4.php. Note that we have changed the method to post.

As we are not sending the variables and their values in the query string, they no longer appear in PHP's $_GET array. They are placed into another array reserved for "posted" form variables, called, unsurprisingly, $_POST. So we just change the code to use this new array, as shown in welcome4.php. The form is the same, functionally; the difference is that URL of the page that is loaded upon clicking the "Go, man, go!" button will not have a query string. Now, if you were to bookmark that page, it would therefore be useless, as the URL you were bookmarking wouldn't contain the values. When you use Google or another search engine, you will see the query string, and if you bookmark the results page, you can use the bookmark to perform the search again. Try it.

And sometimes you want access to a variable without worrying about whether it was sent as a post or as part of the query string. Well, PHP has just what you need! Yessir, here's where the $_REQUEST array comes in right handy! Let's modify welcome4.php so that it can receive the first and last names from either source. Let's call it...welcome5.php. We will also create welcome5.html to refer to welcome5.php.