Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [204]

By Root 1639 0
” dir=”ltr” xmlns=”http://www.w3.org/1999/xhtml”>

greetUser.php

$userName = $_REQUEST[”userName”];

print ”

Hi, $userName!

?>

greetUser.php is not a complex program, but it shows the most common use of PHP: retrieving data from a form. Here’s how you build it:

1. Build a new PHP program.

This program should be in the same directory as askName.html, which should be somewhere the server can find (usually under the htdocs or public_html directory).

2. Start with ordinary XHTML.

PHP programs are usually wrapped inside ordinary XHTML, so begin the document as if it were plain XHTML. Use whatever CSS styling and ordinary HTML tags you want. (I’m keeping this example as simple as possible, although I’d normally add some CSS styles to make the output less boring.)

3. Add a PHP segment.

Somewhere in the page, you’ll need to switch to PHP syntax so that you can extract the data from the form. Use the

$userName = $_REQUEST[“userName”];

print “

Hi, $userName!

”;

?>

4. Extract the username variable.

PHP stores all the data sent to the form inside a special variable called $_REQUEST. This object contains a list of all the form elements in the page that triggered this program. In this case, I want to extract the value of the userName field and store it in a PHP variable called $userName:

$userName = $_REQUEST[“userName”];

See the upcoming section “Getting data from the form” for more information on the $_REQUEST object and some of the other tools that are available for retrieving information.

5. Print the greeting.

Now, your PHP program has a variable containing the user’s name, so you can print a greeting to the user. Remember that all output of a PHP program is XHTML code, so be sure to embed your output in a suitable XHTML tag. I’m putting the greeting inside a level-one heading:

print “

Hi, $userName!

”;

The greetUser.php script is not meant to be run directly. It relies on askName.html. If you provide a direct link to greetUser.php, the program will run, but it will not be sent the username, so it will not work as expected. Do not place links to your PHP scripts unless you designed them to work without input.


Choosing the Method of Your Madness

The key to server-side processing is adding method and action properties to your XHTML form. You have two primary choices for the method property:

♦ get: The get method gathers the information in your form and appends it to the URL. The PHP program extracts form data from the address. The contents of the form are visible for anyone to see.

♦ post: The post method passes the data to the server through a mechanism called environment variables. This mechanism makes the form elements slightly more secure because they aren’t displayed in public as they are with the get method.

Using get to send data

The get method is easy to understand. View getRequest.php after it has been called from askName.html in Figure 2-4. Pay careful attention to the URL in the address bar.

Figure 2-4: The address has been modified!

The address sent to the PHP program has additional material appended:

http://localhost/xfd/ar/xfd5.3_AR_AH/greetUser.php?userName=Andy%20Harris

Most of this address is the (admittedly convoluted) address of the page on my test server. The interesting part is the section after greetUser.php:

greetUser.php?userName=Andy%20Harris

This line shows exactly how the get method passes information to the program on the server:

♦ The URL is extracted from the form action property. When the submit button is activated, the browser automatically creates a special URL beginning with the action property of the form. The default address is the same directory as the original XHTML file.

♦ A question mark indicates form data is on the way. The browser appends a question mark to

Return Main Page Previous Page Next Page

®Online Book Reader