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 />

Server, gimme some r<strong>and</strong>om numbers!<br />

<br />

<br />

<br />

3. In the same folder, create smartproxyping.js:<br />

// holds an instance of XMLHttpRequest<br />

var xmlHttp = createXmlHttpRequestObject();<br />

// holds the remote server address <strong>and</strong> parameters<br />

var serverAddress = "smartproxyping.php";<br />

var getNumberParams = "action=GetNumber" + // get a new r<strong>and</strong>om number<br />

"&min=1" + // the min number to generate<br />

"&max=100"; // the max number to generate<br />

var checkAvailabilityParams = "action=CheckAvailability";<br />

// variables used to check for server availability<br />

var requestsCounter = 0; // counts how many numbers have been retrieved<br />

var checkInterval = 10; // counts interval for checking server<br />

availability<br />

var updateInterval = 1; // how many seconds to wait to get a new number<br />

var updateIntervalIfServerBusy = 10; // seconds to wait when server busy<br />

var minServerBufferLevel = 50; // what buffer level is considered acceptable<br />

// creates an XMLHttpRequest instance<br />

function createXmlHttpRequestObject()<br />

{<br />

// will store the reference to the XMLHttpRequest object<br />

var xmlHttp;<br />

// this should work for all browsers except IE6 <strong>and</strong> older<br />

try<br />

{<br />

// try to create XMLHttpRequest object<br />

xmlHttp = new XMLHttpRequest();<br />

}<br />

catch(e)<br />

{<br />

// assume IE6 or older<br />

var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",<br />

"MSXML2.XMLHTTP.5.0",<br />

"MSXML2.XMLHTTP.4.0",<br />

"MSXML2.XMLHTTP.3.0",<br />

"MSXML2.XMLHTTP",<br />

"Microsoft.XMLHTTP");<br />

// try every prog id until one works<br />

for (var i=0; i

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

Saved successfully!

Ooh no, something went wrong!