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.

<strong>AJAX</strong> <strong>and</strong> the Future of Web Applications}alert("Error creating the XMLHttpRequest object.");elsereturn xmlHttp;// make asynchronous HTTP request using the XMLHttpRequest objectfunction process(){// proceed only if the xmlHttp object isn't busyif (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){// retrieve the name typed by the user on the formname = encodeURIComponent(document.getElementById("myName").value);// execute the quickstart.php page from the serverxmlHttp.open("GET", "quickstart.php?name=" + name, true);// define the method to h<strong>and</strong>le server responsesxmlHttp.onreadystatechange = h<strong>and</strong>leServerResponse;// make the server requestxmlHttp.send(null);}else// if the connection is busy, try again after one secondsetTimeout('process()', 1000);}// executed automatically when a message is received from the serverfunction h<strong>and</strong>leServerResponse(){// move forward only if the transaction has completedif (xmlHttp.readyState == 4){// status of 200 indicates the transaction completed successfullyif (xmlHttp.status == 200){// extract the XML retrieved from the serverxmlResponse = xmlHttp.responseXML;// obtain the document element (the root element) of the XML structurexmlDocumentElement = xmlResponse.documentElement;// get the text message, which is in the first child of// the the document elementhelloMessage = xmlDocumentElement.firstChild.data;// update the client display using the data received from the serverdocument.getElementById("divMessage").innerHTML ='' + helloMessage + '';// restart sequencesetTimeout('process()', 1000);}// a HTTP status different than 200 signals an errorelse{alert("There was a problem accessing the server: " +xmlHttp.statusText);}}}4. Create a file called quickstart.php <strong>and</strong> add the following code to it:

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

Saved successfully!

Ooh no, something went wrong!