13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

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

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

Fundamental Ajax<br />

863<br />

function getServerTime() {<br />

var thePage = 'servertime.php';<br />

myR<strong>and</strong> = parseInt(Math.r<strong>and</strong>om()*999999999999999);<br />

var theURL = thePage +"?r<strong>and</strong>="+myR<strong>and</strong>;<br />

myReq.open("GET", theURL, true);<br />

myReq.onreadystatechange = theHTTPResponse;<br />

myReq.send(null);<br />

}<br />

The first line in the function creates a variable called thePage with a value of servertime.php.This<br />

is the name of the <strong>PHP</strong> script that will reside on your server.<br />

The next line may seem out of place, as it creates a r<strong>and</strong>om number.The obvious<br />

question is “What does a r<strong>and</strong>om number have to do with getting the server time?”The<br />

answer is that it doesn’t have any direct effect on the script itself.The reason the r<strong>and</strong>om<br />

number is created, <strong>and</strong> then appended to the URL in the third line of the function, is to<br />

avoid any problems with the browser (or a proxy) caching the request. If the URL were<br />

simply http://yourserver/yourscript.php, the results might be cached. However, if the<br />

URL is http://yourserver/yourscript.php?r<strong>and</strong>=r<strong>and</strong>val, there isn’t anything to cache<br />

because the URL will be different every time, although the functionality of the underlying<br />

script will not change.<br />

The final three lines of the function use three methods (open, onreadystatechange,<br />

<strong>and</strong> send) of the instance of the XMLHTTPRequest object created by calling<br />

getXMLHTTPRequest() as seen in the previous section.<br />

In the line using the open method, the parameters are the type of request (GET), the<br />

URL (theURL), <strong>and</strong> a value of true indicating that the request is to be asynchronous.<br />

In the line using the onreadystatechange method, the function will invoke a new<br />

function, theHTTPResponse, when the state of the object changes.<br />

In the line using the send method, the function sends NULL content to the server-side<br />

script.<br />

At this point, create a file called servertime.php containing the code in Listing 34.1.<br />

Listing 34.1<br />

The Contents of servertime.php<br />

<br />

This script gets the current server time, through the use of the date() function in <strong>PHP</strong>,<br />

<strong>and</strong> returns this value within an XML-encoded string. Specifically, the date() function<br />

is called twice; once as date('H:i:s'), which returns the hours, minutes, <strong>and</strong> seconds of

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

Saved successfully!

Ooh no, something went wrong!