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.

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

What Just Happened?<br />

Most of the principles implemented in the application were covered earlier in the book, so we will<br />

quickly analyze what's new here, starting from the client-side code. The novelty in index.html<br />

consists in using the element to offer a minimal support for browsers that don't<br />

support JavaScript, or for ones whose JavaScript support has been disabled:<br />

118<br />

<br />

<br />

<br />

This example requires a JavaScript-enabled browser!<br />

<br />

<br />

Browsers that have JavaScript enabled will ignore everything between <strong>and</strong><br />

, while the others will parse <strong>and</strong> display that HTML code.<br />

The client-side JavaScript file, friendly.js has a few surprises of its own:<br />

• We grouped common functionality that h<strong>and</strong>les displaying user messages into the<br />

display <strong>and</strong> displayError functions. Both receive as parameter the message to be<br />

displayed, but displayError displays the message only if debugMode is true (this<br />

variable is defined at the beginning of the file).<br />

• displayError is called in the catch blocks after an exception has been thrown<br />

somewhere, <strong>and</strong> it uses setTimeout to restart the sequence that makes server<br />

requests. You can set how much time the script should wait before attempting<br />

a new server request when an error happens by modifying the value of the<br />

errorRetryInterval variable.<br />

• You can change how often the news message should be displayed by changing the<br />

updateInterval variable.<br />

• In getNews(), we have a simplistic mechanism that checks whether the text received<br />

from the server was a server-side error instead of the message we are waiting for.<br />

This mechanism verifies if the response contains "ERRNO" (which is generated by<br />

our server-side custom error h<strong>and</strong>ler), or "error" (which is generated automatically<br />

by <strong>PHP</strong> in the case of fatal errors or parse errors), or if the response is empty (if the<br />

displayErrors option is set to Off in php.ini, no error text is generated). In any<br />

of these cases, we throw an error manually, which is then received by our<br />

error-h<strong>and</strong>ling mechanism that informs the users that an error has happened.<br />

At the server side, everything starts in friendly.php, which is called from the client. The most<br />

important part of friendly.php is the one where it creates a new instance of the Friendly class<br />

(defined in friendly.class.php), <strong>and</strong> calls its getNews method:<br />

// read the action parameter<br />

$action = $_GET['action'];<br />

// get news<br />

if ($action == 'GetNews')<br />

{<br />

// create new instance of the Friendly class<br />

$friendly = new Friendly();<br />

// use Friendly functionality to retrieve the news message<br />

$news = $friendly->getNews();<br />

// echo the message to be read by the client<br />

echo $news;<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!