13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

856 Chapter 34 Building <strong>Web</strong> 2.0 Applications with Ajax<br />

What Is Ajax?<br />

Ajax itself is not a programming language or even a single technology. Instead, Ajax programming<br />

typically combines client-side JavaScript programming with XML-formatted<br />

data transfers <strong>and</strong> server-side programming via languages such as <strong>PHP</strong>. Additionally,<br />

XHTML <strong>and</strong> CSS are used for presentation of Ajax-enabled elements.<br />

The result of Ajax programming is typically a cleaner <strong>and</strong> faster user interface to an<br />

interactive application—think of the interfaces to Facebook, Flickr, <strong>and</strong> other sorts of<br />

social networking sites that are at the forefront of <strong>Web</strong> 2.0.These applications enable the<br />

user to perform many tasks without reloading or redrawing entire pages, <strong>and</strong> this is<br />

where Ajax comes into play. Client-side programming invokes a bit of server-side programming,<br />

but only in a specific area displayed in the user’s browser, which is then the<br />

only area to be redrawn.This action mimics the result of actions in st<strong>and</strong>alone applications,<br />

but in a web environment.<br />

A common example is that of working in a spreadsheet application (offline) versus<br />

viewing a table full of information on a website. In the offline application, the user could<br />

make changes in one cell <strong>and</strong> have formulas applied to other cells, or the user could sort<br />

the data in one column, all without leaving the original interface. In a static web environment,<br />

clicking a link to sort a column would require a new request to the server, a<br />

new result sent to the browser, <strong>and</strong> for the page to be redrawn to the user. In an Ajaxenabled<br />

web environment, that table could be sorted based on the user’s request, but<br />

without reloading the entire page.<br />

In the next few sections, we look at the various technologies that come into play<br />

when using Ajax.This information is by no means comprehensive; I provide additional<br />

resources throughout.<br />

HTTP Requests <strong>and</strong> Responses<br />

Hypertext Transfer Protocol, or HTTP, is an Internet st<strong>and</strong>ard defining the way web<br />

servers <strong>and</strong> web browsers communicate with each other.When a user requests a web<br />

page by typing a URL into the location bar of a web browser, or by following a link,<br />

submitting a form, or performing any other task that takes the user to a new destination,<br />

the browser makes an HTTP request.<br />

This request is sent to a web server, which returns one of many possible responses.To<br />

get an underst<strong>and</strong>able response from the web server, the request has to be properly<br />

formed. Knowing the proper formation of requests <strong>and</strong> responses is critical when using<br />

Ajax, because it is the responsibility of the developer to write HTTP requests <strong>and</strong> expect<br />

certain results within the Ajax application.<br />

When making an HTTP request, the client sends information in the following<br />

format:<br />

n The opening line, which contains the method, the path to the resource, <strong>and</strong> the<br />

HTTP version in use, such as the following:<br />

GET http://server/phpmysql4e/chapter34/test.html HTTP/1.1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!