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.

Client-Side Techniques with Smarter JavaScript<br />

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

xmlHttp.send(null);<br />

Note that you cannot load the script locally, directly from the disk using a file://<br />

resource. Instead, you need to load it through HTTP. To load it locally, you would need<br />

to mention the complete access path to the .txt file, <strong>and</strong> in that case you may meet a<br />

security problem that we will deal with later.<br />

Supposing that the HTTP request was successfully initialized <strong>and</strong> executed asynchronously, the<br />

h<strong>and</strong>leRequestStateChange method will get called every time the state of the request changes. In<br />

real applications we will ignore all states except 4 (which signals the request has completed), but<br />

in this exercise we print a message with each state so you can see the callback method actually<br />

gets executed as advertised.<br />

The code in h<strong>and</strong>leRequestStateChange is not that exciting by itself, but the fact that it's being<br />

called for you is very nice indeed. Instead of waiting for the server to reply with a synchronous<br />

HTTP call, making the request asynchronously allows you to continue doing other tasks until a<br />

response is received.<br />

The h<strong>and</strong>leRequestStateChange function starts by obtaining a reference to the HTML element<br />

called myDivElement, which is used to display the various states the HTTP request is going through:<br />

54<br />

// function that h<strong>and</strong>les the HTTP response<br />

function h<strong>and</strong>leRequestStateChange()<br />

{<br />

// obtain a reference to the element on the page<br />

myDiv = document.getElementById("myDivElement");<br />

// display the status o the request<br />

if (xmlHttp.readyState == 1)<br />

{<br />

myDiv.innerHTML += "Request status: 1 (loading) ";<br />

}<br />

else if (xmlHttp.readyState == 2)<br />

...<br />

...<br />

When the status hits the value of 4, we have the typical code that deals with reading the server<br />

response, hidden inside xmlHttp.ResponseText:<br />

// when readyState is 4, we also read the server response<br />

else if (xmlHttp.readyState == 4)<br />

{<br />

// continue only if HTTP status is "OK"<br />

if (xmlHttp.status == 200)<br />

{<br />

try<br />

{<br />

// read the message from the server<br />

response = xmlHttp.responseText;<br />

// display the message<br />

myDiv.innerHTML += "Request status: 4 (complete). Server said: ";<br />

myDiv.innerHTML += response;<br />

}<br />

catch(e)<br />

{<br />

// display error message<br />

alert("Error reading the response: " + e.toString());<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!