27.10.2015 Views

AJAX and PHP

Create successful ePaper yourself

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

T<br />

T<br />

Client-Side Techniques with Smarter JavaScript<br />

The third parameter of open, called async, specifies whether the request should be h<strong>and</strong>led<br />

asynchronously; true means that script processing carries on after the send() method returns<br />

without waiting for a response; false means that the script waits for a response before<br />

continuing processing, freezing the web page functionality. To enable asynchronous processing,<br />

you will seed to set async to true, <strong>and</strong> h<strong>and</strong>le the onreadystatechange event to process the<br />

response from the server.<br />

When using GET to pass parameters, you send the parameters using the URL's query string, as<br />

in http://localhost/ajax/test.php?param1=x&param2=y. This server request passes two<br />

parameters—a parameter called param1 with the value x, <strong>and</strong> a parameter called param2 with the<br />

value y.<br />

// call the server page to execute the server side operation<br />

xmlHttp.open("GET", "http://localhost/ajax/test.php?param1=x&param2=y", true);<br />

xmlHttp.onreadystatechange = h<strong>and</strong>leRequestStateChange;<br />

xmlHttp.send(null);<br />

When using POST, you send the query string as a parameter of the send method, instead of joining<br />

it on to the base URL, like this:<br />

// call the server page to execute the server side operation<br />

xmlHttp.open("POST", "http://localhost/ajax/test.php", true);<br />

xmlHttp.onreadystatechange = h<strong>and</strong>leRequestStateChange;<br />

xmlHttp.send("param1=x&param2=y");<br />

The two code samples should have the same effects. In practice, using GET can help with<br />

debugging because you can simulate GET requests with a web browser, so you can easily see with<br />

your own eyes what your server script generates. The POST method is required when sending data<br />

larger than 512 bytes, which cannot be h<strong>and</strong>led by GET.<br />

In our examples, we will place the code that makes the HTTP request inside a function called<br />

process() in the JavaScript file. The minimal implementation, which is quite fragile <strong>and</strong> doesn't<br />

implement any error-h<strong>and</strong>ling techniques, looks like this:<br />

function process()<br />

{<br />

// call the server page to execute the server side operation<br />

xmlHttp.open("GET", "server_script.php", true);<br />

xmlHttp.onreadystatechange = h<strong>and</strong>leRequestStateChange;<br />

xmlHttp.send(null);<br />

}<br />

This method has the following potential problems:<br />

• process() may be executed even if xmlHttp doesn't contain a valid<br />

XMLHttpRequest instance. This may happen if, for example, the user's browser<br />

doesn't support XMLHttpRequest. This would cause an unh<strong>and</strong>led exception to<br />

happen, so our other efforts to h<strong>and</strong>le errors don't help very much if we aren't<br />

consistent <strong>and</strong> do something about the process function as well.<br />

• process() isn't protected against other kinds of errors that could happen. For<br />

example, as you will see later in this chapter, some browsers will generate a security<br />

exception if they don't like the server you want to access with the XMLHttpRequest<br />

object (more on security in Chapter 3).<br />

48<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!