Online Book Reader

Home Category

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

By Root 1382 0
there are a few special steps to take:

1. Write an XHTML page as the framework.

This page is a regular XHTML page. Begin with the same XHTML framework you use for building your standard XHTML pages. You can use CSS styles, if you wish (but I’m leaving them out of this simple example).

Normally, you can create an XHTML document anywhere you want, but this is not so when your page will be working with PHP. This page is meant to be paired with a PHP document. PHP documents will run only if they are in a server’s file space, so you should save your XHTML document under htdocs to be sure it will call the PHP form correctly.

2. Set the form’s action property to point to a PHP program.

The form element has an attribute called action. The action attribute is used to determine which program should receive the data transmitted by the form. I want this data to be processed by a program called greetUser.php, so I set greetUser.php as the action:

method = “get”>

3. Set the form’s method attribute to get.

The method attribute indicates how the form data will be sent to the server. For now, use the get method. See the section “Choosing the Method of Your Madness,” later in this chapter, for information on the various methods available:

method = “get”>

4. Add any input elements your form needs.

The point of a form is to get information from the user and send it to a program on the server. Devise a form to ask whatever questions you want from the server. My form is as simple as possible, with one text field, but you can use any XHTML form elements you want:

method = “get”>

name = “userName” />

5. Give each element a name attribute.

If you want a form element to be passed to the server, you must give it a name attribute. Note: This is a different attribute than id, which is used in client-side processing.

name = “userName” />

The name attribute will be used by the PHP program to extract the information from the form.

A form element can have both a name and an ID, if you wish. The name attribute will be used primarily by server-side programs, and the id attribute is mainly used for CSS and JavaScript. The name and ID can (and probably should) have the same value.

6. Add a submit button to the page.

The most important difference between a client-side form and a form destined for processing on the server is the button. A special submit button packages all the data in the form and passes it to the program indicated in the action property. Submit buttons can be created in two forms:

or

Specify submit as the button’s type attribute to ensure the button sends the data to the server.

If your form has a submit button and a blank action attribute, the current page will be reloaded.


Receiving data in PHP

PHP code is usually a two-step process. First, you create an XHTML form, and then you send that form to a PHP program for processing. Be sure to read the previous section on “Creating a form for PHP processing” because now I show you how to read that form with a PHP program.

The XHTML form in the last section pointed to a program named greetUser.php. This tells the server to go to the same directory that contained the original XHTML document (askName.html) and look for a program named greetUser.php in that directory. Because greetUser is a PHP program, the server passes it through PHP, which will extract data from the form. The program then creates a greeting using data that came from the form. Look over all the code for greetUser.php before I explain it in more detail:

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

®Online Book Reader