beta it republik » Book Reviews

Books

Untitled Document

AJAX and PHP: Building Responsive Web Applications

Reviewed by Stoyan Stefanov


  • Author/in: Cristian Darie, Bogdan Brinzarea, Filip CherecheŞ-ToŞa, Mihai Bucica
  • Publisher: Packt Publishing
  • Pages: 275
  • Year of Publication: 2006
  • Price:
  • ISBN:1-90481-182-5

This book is a practical introduction to creating more responsive web pages using Ajax with PHP and MySQL on the server side. Every chapter is a solution to a problem (such as creating a chat application or auto-complete input fields) and goes through defining the problem, providing the solution and then explaining it, accenting on the most interesting parts of the code.

Part I – Introduction

The first chapter provides some historical background of web applications, the request-response nature of the HTTP protocol and mentions how Asynchronous JavaScript and XML (Ajax) builds on that to provide for more responsive web pages using small requests and updating parts of the page.

As the book puts it, Ajax allows the client/browser and the server to communicate in the background, while the user is working on the page. The chapter ends with a simple Ajax example, which may come as a challenge to a novice reader, but it encourages you to get involved early. The next chapter goes in a much slower pace through JavaScript, XML (Extensible Markup Language), DOM (Document Object Model) and a detailed explanation of the XMLHttpRequest part, the most important component of any Ajax application.

Chapter 3 shows how PHP gets involved into the Ajax picture The chapter elaborates how PHP can be used to:
• Generate XML through DOM on the server side (Note: Chapter 1 talks about DOM on the client side)

• Connect to a data storage, using a MySQL database as example
• Serve as a proxy to accessing data located on different domains, since XMLHttpRequest can be made to the own domain only

The chapter ends with a quick MySQL introduction, showing how to create tables, store data in them, and execute queries on that data.

The first part thus aims to arm the reader with an understanding of important concepts and a framework for making asynchronous requests that makes diving into the examples much more easier. Further, all chapters pay special attention to security concerns and error handling, on both the client and the server side. Although you may be tempted to skip these sections to get to the more visible aspects of the Ajax development, they are a definite value add.

Part 2 – Solutions

New concepts and technologies, such as Extensible Stylesheet Language Transformations (XSLT) and Scalable Vector Graphics (SVG), are introduced in the second part, to round off the reader’s knowledge on the contemporary web application building blocks.

The rest of the book adopts a how-to style to explain the following:

• Form validation (That is, sending mini-requests to the server as the user fills out a form and receiving the server response whether values entered in the form are acceptable.) This allows, for example, checking against the database whether a username is available, without reloading the whole page. The author explains an unobtrusive approach for form validation so it works even when JavaScript is disabled on the client side.

• Creating a chat application in a thread-safe fashion that deals with the inevitable network lags, to build a solution that behaves as expected.

• Adding a caching technique on top of basic Autocomplete (as seen on Google Suggest) functionality, to make as less
new requests as possible.

• Creating a real-time updated SVG chart. The chapter provides a working example plus ideas on where to look for more.

• Building an editable data grid that allows for navigating (using pagination) and inline editing of a whole database table without page reloads. A cross-browser technique to do XSL transformations on the client is also presented.

• Coding a page to syndicate Really Simple Syndication (RSS) feeds. Again, XSL transformations are demonstrated, but this time on the server side.

• Using the script.aculo.us[1] JavaScript library to create a sortable, drag-and-drop-enabled shopping cart

Extras

The book has three appendices. The first one is part of the printed copy and shows you how to install and set up the environment needed to run the examples – on Windows and Linux.

Appendix B, which is available online, provides points on selecting the right tools for the job (such as editors and browser extensions). Appendix C, also available online, is an introduction to XML, XSLT, and Xpath.

Rounding up the online resources is a small application, Ajax Whiteboard, which allows for collaborative drawings with the help of SVG.

Now for the Actual Review

The language used in the book is simple and clear. New concepts are introduced in a manner that makes them easy to understand and encourage you to experiment with the practical examples. Of course, the main skill you will master by working through the book is the use of XMLHttpRequest.

Content Age

The main concern for any Ajax book is that the content may age quickly. Since the topic is relatively new, new and improved ways of doing things are constantly being discovered, and new libraries and frameworks are released incessantly.

This book teaches you the basics that are unlikely to change, considering that the technologies associated with Ajax are
relatively old. So, even if you choose utilities such as Prototype[2] or YUI[3] to do low-level requests for you, the book can help you understand the type of work being performed on your behalf.

Code

Due to the fact that the code is given in its entirety in every example, some pieces of code (such as the creation of XMLHttpRequest objects) repeat in every example. The authors could have opted for a few reusable functions that are defined and explained only once in the book.

Coding Conventions

The four authors stick to using the same coding conventions throughout the book, with the exception of the last chapter where the author has chosen not to wrap one-liner for loops in curly brackets.

PHP 5

PHP 5 is a requirement for the examples in the book. While the world may become a better place if everyone (developers and hosting providers alike) adopts PHP 5, the book would’ve done well to include equivalent PHP4 code, at least as a download on the publisher’s web site.

Appendix A gives instructions on how to set up the environment. So you should have no trouble running the examples on your local computer. But when you decide to create live sites based on the examples in the book, you may have an issue with your hosting provider.

Web Standards

The entire markup in the examples is valid XHTML 1.0. Well, with a pedantic eye, I spotted one or two exceptions where
<input>s were not in a <form>.

Another area concerning standards was the use of innerHTML. Although the book teaches you how to create new page elements the ‘proper’ way (using DOM functions), sometimes non-standard innerHTML is used as well.

What is Missing? JSON

Although JavaScrpt Object Notation (JSON)[4] is mentioned as a possible data transfer option (instead of XML), there is no example of its use later in the book.

Another use of JSON is to structure JavaScript code, by encapsulating it in objects and namespacing it. In the book, however, all JavaScript functions are in the global namespace; these would have benefited from a little JSON touch.

Note: While this review was written, an update to the book was released and that is a new version of Chapter 5, the Ajax chat. It now uses the JSON format for data transfer.

Conclusion

I enjoyed the book and recommend it to those looking to add Ajax to their web development skills. The book includes a lot of condensed information, with practical and useful examples, while also directing you on where to look for more.

To summarize, on the client side you’ll learn:

• Creating XMLHttpRequests in a cross-browser fashion
• Making HTTP requests with JavaScript and handling the response
• Using JavaScript to dynamically update parts of the page using DOM and using innerHTML
• XSL transformations on the client
• Working with SVG, and using JavaScript to make requests and updates from within an SVG file
• Using script.aculo.us for sortable lists and drag and drop functionality

On the server side you’ll learn how to:

• Handle server side errors elegantly
• Work with MySQL using the object-oriented interface of the mysqli[5] extension
• Generating XML using DOM[6] and using the plain old string concatenation
• XSL transformations on the server
• Using the server as a proxy to access content on other domains



Commment



Name:




Comment:

Captcha Verification !
captcha_image