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.

<strong>AJAX</strong> Suggest <strong>and</strong> Autocomplete<br />

10. Create another file named suggest.js, <strong>and</strong> add this code to it:<br />

/* URL to the <strong>PHP</strong> page called for receiving suggestions for a keyword*/<br />

var getFunctionsUrl = "suggest.php?keyword=";<br />

/* URL for seeing the results for the selected suggestion */<br />

var phpHelpUrl="http://www.php.net/manual/en/function.";<br />

/* the keyword for which an HTTP request has been initiated */<br />

var httpRequestKeyword = "";<br />

/* the last keyword for which suggests have been requested */<br />

var userKeyword = "";<br />

/* number of suggestions received as results for the keyword */<br />

var suggestions = 0;<br />

/* the maximum number of characters to be displayed for a suggestion */<br />

var suggestionMaxLength = 30;<br />

/* flag that indicates if the up or down arrow keys were pressed<br />

the last time a keyup event occurred */<br />

var isKeyUpDownPressed = false;<br />

/* the last suggestion that has been used for autocompleting the keyword<br />

*/<br />

var autocompletedKeyword = "";<br />

/* flag that indicates if there are results for the current requested<br />

keyword*/<br />

var hasResults = false;<br />

/* the identifier used to cancel the evaluation with the clearTimeout<br />

method. */<br />

var timeoutId = -1;<br />

/* the currently selected suggestion (by arrow keys or mouse)*/<br />

var position = -1;<br />

/* cache object containing the retrieved suggestions for different<br />

keywords */<br />

var oCache = new Object();<br />

/* the minimum <strong>and</strong> maximum position of the visible suggestions */<br />

var minVisiblePosition = 0;<br />

var maxVisiblePosition = 9;<br />

// when set to true, display detailed error messages<br />

var debugMode = true;<br />

/* the XMLHttp object for communicating with the server */<br />

var xmlHttpGetSuggestions = createXmlHttpRequestObject();<br />

/* the onload event is h<strong>and</strong>led by our init function */<br />

window.onload = init;<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!