HTML and CSS Reference
In-Depth Information
18.4 Putting It All Together
The steps we have covered are summarized in the following Ajax examples to demon-
strate how to communicate with the server. The first example demonstrates how to use
Ajax to get the server's time and respond to key events, the next two examples work with
a text file and an XML file, and demonstrate how to retrieve display the content of both
types of files, and the last example uses Ajax with forms using both the GET and POST
methods. All of the programs go through the following steps:
1. The user clicks a button or presses a key to initiate a function that will start the
process of Ajax communicating with the server.
2. Most important, JavaScript uses the XMLHttpRequest constructor method to
create a new object that will serve as the Ajax communication layer between the
browser and the server. (If not this, IFrames or cookies can be used, but will not
be covered in this text.)
3. Once the XMLHttpRequest object is created, it is initialized with the object's
open() method to set up the type of HTTP request (GET or POST), the URL
(where the request is going), and whether the request will be asynchronous
(true and the default), or synchronous (false).
4. The request is then sent to the server with the object's send() method.
5. The server processes the request and sends a response to the browser in either
XML or text format. It contains the data only of the page elements that need to
be changed. In most cases this data will include of just a fraction of the total
page markup.
6. JavaScript processes the server response, updates the relevant page content, or
performs another operation with the new data received from the server.
Connecting to a Server Program. If you are ready to start using PHP and Apache
to test these examples, go to and download
XAMPP. It is a very easy to install Apache Distribution for Linux, Solaris, Windows and
Mac OS X. The package includes the Apache Web server, MySQL, PHP, Perl, an FTP
server, and phpMyAdmin. Once you have installed XAMPP, you will be able to start up
the services with the XAMPP control panel by clicking on the XAMPP icon (see Figure
18.7) or going to the Start menu (Windows) or Application menu (MacOS). When you
get to the XAMPP control panel (see Figure 18.8), just click to start Apache to get your
Apache HTTP server running. PHP scripts will be stored in the same place as HTML
files, under the server's root, and should be given a .php extension. There are many tuto-
rials and books on PHP. Of course we recommend PHP and MySQL by Example by Ellie
Quigley and Marko Gargenta (Prentice Hall, ISBN 0-13-187508-6).
Figure 18.7 XAMPP icon to open the control panel.
Search WWH ::

Custom Search