Online Book Reader

Home Category

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

By Root 1359 0
When the PHP code is finished, it will be replaced by XHTML code.

Viewing the results

If you view showDate.php in your browser, you won’t see the PHP code. Instead, you’ll see an XHTML page. It’s even more interesting when you use your browser to view the page source. Here’s what you’ll see:

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

showDate.php

Getting the Time, PHP Style

Date: 02-13

Time: 10:02

The remarkable thing is what you don’t see. When you look at the source of showDate.php in your browser, the PHP is completely gone! This is one of the most important points about PHP: The browser never sees any of the PHP. The PHP code is converted completely to XHTML before anything is sent to the browser. This means that you don’t need to worry about whether a user’s browser understands PHP. Because the user never sees your PHP code (even if he views the XHTML source), PHP code will work on any browser.

Exploring the date() format function

The showDate.php program takes advantage of one of PHP’s many interesting and powerful functions to display the date. The PHP date() function returns the current date. Generally, you’ll pass the date() function a special format string that indicates how you want the date to be formatted. Characters in the date string indicate a special code. Here are a few of the characters and their meanings:

d: day of the month (numeric)

D: three character abbreviation of weekday (Wed)

m: month (numeric)

M: three-character abbreviation of month (Feb)

F: text representation of month (February)

y: two-digit representation of the year (08)

Y: four-digit representation of the year (2008)

h: hour (12 hours)

H: hour (24 hours)

i: minutes

s: seconds

You can embed standard punctuation in the format as well, so d/m/y will include the slashes between each part of the date. There are many more symbols available. Check the PHP documentation at http://us3.php.net/manual/en/

function.date.php for more information about date and time formatting.


Sending Data to a PHP Program

You can send data to a PHP program from an HTML form. For an example of this technique, see askName.html in Figure 2-2.

Figure 2-2: This XHTML page has a simple form.

XHTML forms (described fully in Book I, Chapter 7) allow the user to enter data onto a Web page. However, XHTML cannot respond to a form on its own. You need some sort of program to respond to the form. Book IV describes how to use JavaScript to respond to forms, but you can also write PHP code to handle form-based input. When the user submits the form, the askName.html disappears completely from the browser and is replaced with greetUser.php, as shown in Figure 2-3.

Figure 2-3: This program uses the entry from the previous form.

The greetUser.php program retrieves the data from the previous page (askName.html, in this case) and returns an appropriate greeting.


Creating a form for PHP processing

The askName.html program is a standard XHTML form, but it has a couple of special features which make it suitable for PHP processing. (See Book I, Chapter 7 for more information about how to build XHTML forms.) Here is the XHTML code:

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

askName.html

method = “get”>

name = “userName” />

To build a form designed to work with PHP,

Return Main Page Previous Page Next Page

®Online Book Reader