27.10.2015 Views

AJAX and PHP

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Server-Side Techniques with <strong>PHP</strong> <strong>and</strong> MySQL<br />

}<br />

}<br />

{<br />

}<br />

// display status message<br />

alert("There was a problem retrieving the data:\n" +<br />

xmlHttp.statusText);<br />

// h<strong>and</strong>les the response received from the server<br />

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

{<br />

// retrieve the server's response<br />

var response = xmlHttp.responseText;<br />

// if the response is longer than 3 characters, or if it is void, we<br />

// assume we just received a server-side error report<br />

if(response.length > 3 || response.length == 0)<br />

throw(response.length == 0 ? "Server error" : response);<br />

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

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

// display the HTML output<br />

myDiv.innerHTML = "Server says: " + response + "";<br />

}<br />

4. Build the hero proxy <strong>PHP</strong> script, proxyping.php:<br />

<br />

5. Finally, add the error-h<strong>and</strong>ler function. Yes, it's a bit more to type, but it does good<br />

things to your solution (you can copy <strong>and</strong> paste it from other examples, because it<br />

is not going to change). Create a new file named error_h<strong>and</strong>ler.php, <strong>and</strong> write<br />

this code:<br />

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

Saved successfully!

Ooh no, something went wrong!