Online Book Reader

Home Category

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

By Root 1591 0
programmers use a PHP program to request information from a Web page, and then use this information to prepare a request for the database in a special language called SQL (Structured Query Language). The data request is passed to the database management system, which returns some kind of result set to the PHP program. The PHP program then typically builds an HTML page and passes the page back to the browser.

The process can be easier when you use AJAX, because the PHP program doesn’t have to create an entire Web page. All that really needs to be passed back to the JavaScript program is the results of the data query. The examples in this chapter have created XHTML snippets as their output, but you often want to make your server-side programs a little more generic so that the data can be used in a number of different ways. Normally, the data is returned using a special data format so that the JavaScript program can easily manage the data.


Review of XML

The XML format has become an important tool for encapsulating data for transfer between the client and the server. You’re already familiar with XML, because XHTML is simply HTML following the stricter XML standard.

XML is much more than XHTML. XML can actually be used to store any kind of data. For example, take a look at the following file (pets.xml):

cat

Lucy

American Shorthair

She raised me

cat

Homer

unknown

Named after a world-famous bassoonist

dog

Muchacha

mutt

One of the ugliest dogs I’ve ever met

If you look over pets.xml, you can see that it looks a lot like HTML. HTML/XHTML tags are very specific (only a few are legal), but XML tags can be anything, as long as they follow a few simple (but familiar) rules:

1. Begin with a doctype.

Formal XML declarations often have doctypes as complex as the XHTML doctype definition, but basic XML data typically uses a much simpler definition:

Anytime you make your own XML format (as I’m doing in this example), you can use this generic doctype.

2. Create a container for all elements.

The entire structure must have one container tag. I’m using pets as my container. If you don’t have a single container, your programs will often have trouble reading the XML data.

3. Build your basic data nodes.

In my simple example, each pet is contained inside a pet node. Each pet has the same data elements (but that is not a requirement).

Tags are case sensitive. Be consistent in your tag names. Use camel-case and single words for each element.

4. Add attributes as needed.

You can add attributes to your XML elements just like the ones in XHTML. As in XHTML, attributes are name/value pairs separated by an equals sign (=), and the value must always be encased in quotes.

5. Nest elements as you do in XHTML.

Be careful to carefully nest elements inside each other like you do with XHTML.

You can get an XML file in a number of ways:

♦ Most databases can export data in XML format.

♦ More often, a PHP program reads data from a database and creates a long string of XML for output.

For this simple introduction, I just wrote the XML file in a text editor and saved it as a file.

You manipulate XML in the same way with JavaScript, whether it comes directly from a file or is passed from a PHP program.


Manipulating XML with jQuery

XML data is actually familiar, because you can use the tools you used to work with XHTML. Better, the jQuery functions normally used to extract elements from an XHTML page work on XML data with few changes. All the standard jQuery selectors and tools can be used to manage an XML file in the same way that they manage parts of an HTML page.

The readXML.html page featured in Figure 6-4 shows a JavaScript/jQuery program that reads

Return Main Page Previous Page Next Page

®Online Book Reader