![]() Join Up! 96815 members and counting! |
|
|||
Using XML: A PHP Developer's Primer, Part 2
Adam Delves
An Introduction to AJAX
In the first part of this series, we took a look at how PHP 5 can be used to manipulate and parse XML files. In this installment, we are going to focus on Ajax, one of the most useful and topical applications of XML.
Initially, we are going to introduce Ajax and learn how to use
the XMLHTTP object provided by most modern web browsers to create a
live email validation form. Then we will pick up where we
left off with the theme of XML and introduce XSLT, which we will use
to transform our library XML from the previous article into valid XHTML
code.
What is AJAX?
There exists today a huge number
of web applications that do everything from manage your photos, to mission-critical distributed systems. The majority of these applications exist in three parts.
In most web applications, the invocation of logic on the middle ware,
which may or may not change the information presented to the user,
requires that the user click a link or submit data via a form. This in
turn requires that the entire page be refreshed and all data be resent to
the user. This is OK when a large portion of the page is being
refreshed, but it is very wasteful if little or no data is to be updated.
The philosophy behind Ajax is that when a user makes a request to the
server-side part of the application, only the data which is required is
sent back to the user and rather than reloading the entire page, only
the parts which require updating are updated.
Ajax is an acronym for Asynchronous Javascript And XML. It is the use
of Javascript code in HTML pages that allows it to make independent requests to the
web server. XML is by no means mandatory in an Ajax solution and, in
some cases it is completely unnecessary. With Ajax, the front end is
split into two parts: the HTML and CSS which deals with the page
display and the Ajax engine.
The Ajax engine is responsible for
initiating two way communication with the server in response to user
events on the page. It enables developers to enrich and enhance the
usability of web applications by creating powerful event driven web
front ends, similar to the kind that we are accustomed to on the
desktop domain, which respond in almost real time.
You can see a more detailed definition of Ajax and how it fits into the
the web design domain, by reading Chapter 1 of the Ajax
in Action.
Live E-Mail Validation with
AJAX
We are now going to dive into the world of AJAX by creating a simple
application which validates and verifies a user's email address while
they are filling out a web form. A prior knowledge of HTML and
Javascript is beneficial when following this article, however there is
no better way of learning than to manually copy (not copy and paste)
and implement the solution yourself.
Normally, email validation and verification is carried out by the
server. It often involves a syntax check and the sending of an email
containing a link and verification code, which, once clicked, confirms
that the email is real and active.
Our small application will do all of this before the form is
submitted, or more precisely, while it is being filled out. This kind of feature is especially
useful if the form takes several minutes to complete and it can also be
extended to include a randomly generated image in place of a plain text
code in the verification.
To assist in the creation of our small AJAX application, I have created
an Email Validation class, email_validator.php which provides the tools
necessary to validate and verify an email address. It uses a SqLite
database to store email addresses which are to be verified and provides
the necessary methods to check the syntax of the email, validate domain
part of the email and sends a randomly generated verification code the
email address.
The source code for this class and the documentation can be downloaded
in this ZIP file.
Coding for AJAX
It is worth remembering that when
introducing or including Ajax in web
applications, the coding becomes more complex.
The Server-Side PHP
Our Ajax engine's partner in
crime will be a small PHP script called
email_validate.php. It receives the data from the Ajax engine,
processes it by carrying out the required action and sends a plain text
response.
Validating the Data
The data from the Ajax engine
will be passed to our script in the URL
query string. The three values which are sent are as follows:
PHP: /* get the action */
Notice how we have overridden the
action variable to
re-validate the
email address if an email address is sent as well as an ID (indicating
that it has changed). We also override the action variable if an error
occurs when attempting to create an instance of the EmailValidator
object.
|