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> MySQLServer, gimme some r<strong>and</strong>om numbers!3. In the same folder, create smartproxyping.js:// holds an instance of XMLHttpRequestvar xmlHttp = createXmlHttpRequestObject();// holds the remote server address <strong>and</strong> parametersvar serverAddress = "smartproxyping.php";var getNumberParams = "action=GetNumber" + // get a new r<strong>and</strong>om number"&min=1" + // the min number to generate"&max=100"; // the max number to generatevar checkAvailabilityParams = "action=CheckAvailability";// variables used to check for server availabilityvar requestsCounter = 0; // counts how many numbers have been retrievedvar checkInterval = 10; // counts interval for checking serveravailabilityvar updateInterval = 1; // how many seconds to wait to get a new numbervar updateIntervalIfServerBusy = 10; // seconds to wait when server busyvar minServerBufferLevel = 50; // what buffer level is considered acceptable// creates an XMLHttpRequest instancefunction createXmlHttpRequestObject(){// will store the reference to the XMLHttpRequest objectvar xmlHttp;// this should work for all browsers except IE6 <strong>and</strong> oldertry{// try to create XMLHttpRequest objectxmlHttp = new XMLHttpRequest();}catch(e){// assume IE6 or oldervar XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");// try every prog id until one worksfor (var i=0; i

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

Saved successfully!

Ooh no, something went wrong!