11.07.2015 Views

AJAX and PHP

AJAX and PHP

AJAX and PHP

SHOW MORE
SHOW LESS

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> MySQLWhat Just Happened?Most of the principles implemented in the application were covered earlier in the book, so we willquickly analyze what's new here, starting from the client-side code. The novelty in index.htmlconsists in using the element to offer a minimal support for browsers that don'tsupport JavaScript, or for ones whose JavaScript support has been disabled:118This example requires a JavaScript-enabled browser!Browsers that have JavaScript enabled will ignore everything between <strong>and</strong>, while the others will parse <strong>and</strong> display that HTML code.The client-side JavaScript file, friendly.js has a few surprises of its own:• We grouped common functionality that h<strong>and</strong>les displaying user messages into thedisplay <strong>and</strong> displayError functions. Both receive as parameter the message to bedisplayed, but displayError displays the message only if debugMode is true (thisvariable is defined at the beginning of the file).• displayError is called in the catch blocks after an exception has been thrownsomewhere, <strong>and</strong> it uses setTimeout to restart the sequence that makes serverrequests. You can set how much time the script should wait before attemptinga new server request when an error happens by modifying the value of theerrorRetryInterval variable.• You can change how often the news message should be displayed by changing theupdateInterval variable.• In getNews(), we have a simplistic mechanism that checks whether the text receivedfrom the server was a server-side error instead of the message we are waiting for.This mechanism verifies if the response contains "ERRNO" (which is generated byour server-side custom error h<strong>and</strong>ler), or "error" (which is generated automaticallyby <strong>PHP</strong> in the case of fatal errors or parse errors), or if the response is empty (if thedisplayErrors option is set to Off in php.ini, no error text is generated). In anyof these cases, we throw an error manually, which is then received by ourerror-h<strong>and</strong>ling mechanism that informs the users that an error has happened.At the server side, everything starts in friendly.php, which is called from the client. The mostimportant part of friendly.php is the one where it creates a new instance of the Friendly class(defined in friendly.class.php), <strong>and</strong> calls its getNews method:// read the action parameter$action = $_GET['action'];// get newsif ($action == 'GetNews'){// create new instance of the Friendly class$friendly = new Friendly();// use Friendly functionality to retrieve the news message$news = $friendly->getNews();// echo the message to be read by the clientecho $news;}

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

Saved successfully!

Ooh no, something went wrong!