27.10.2015 Views

AJAX and PHP

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

Time for Action—Passing <strong>PHP</strong> Parameters <strong>and</strong> Error H<strong>and</strong>ling<br />

1. In the foundations folder, create a new folder called morephp.<br />

2. In the morephp folder, create a file named morephp.html:<br />

<br />

<br />

<br />

Practical <strong>AJAX</strong>: <strong>PHP</strong> Parameters <strong>and</strong> Error H<strong>and</strong>ling<br />

<br />

<br />

<br />

Ask server to divide<br />

<br />

by<br />

<br />

<br />

<br />

<br />

<br />

3. Create a new file named morephp.js:<br />

// holds an instance of XMLHttpRequest<br />

var xmlHttp = createXmlHttpRequestObject();<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!