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.

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

}<br />

if(response.childNodes.length)<br />

{<br />

/* we retrieve the new functions' names from the document element as<br />

an array */<br />

nameArray= xmlToArray(response.getElementsByTagName("name"));<br />

}<br />

// check to see if other keywords are already being searched for<br />

if(httpRequestKeyword == userKeyword)<br />

{<br />

// display the results array<br />

displayResults(httpRequestKeyword, nameArray);<br />

}<br />

else<br />

{<br />

// add the results to the cache<br />

// we don't need to display the results since they are no longer useful<br />

addToCache(httpRequestKeyword, nameArray);<br />

}<br />

/* populates the list with the current suggestions */<br />

function displayResults(keyword, results_array)<br />

{<br />

// start building the HTML table containing the results<br />

var div = "";<br />

// if the searched for keyword is not in the cache then add it to the cache<br />

if(!oCache[keyword] && keyword)<br />

addToCache(keyword, results_array);<br />

// if the array of results is empty display a message<br />

if(results_array.length == 0)<br />

{<br />

div += "No results found for " + keyword +<br />

"";<br />

// set the flag indicating that no results have been found<br />

// <strong>and</strong> reset the counter for results<br />

hasResults = false;<br />

suggestions = 0;<br />

}<br />

// display the results<br />

else<br />

{<br />

// resets the index of the currently selected suggestion<br />

position = -1;<br />

// resets the flag indicating whether the up or down key has been pressed<br />

isKeyUpDownPressed = false;<br />

/* sets the flag indicating that there are results for the searched<br />

for keyword */<br />

hasResults = true;<br />

// get the number of results from the cache<br />

suggestions = oCache[keyword].length;<br />

// loop through all the results <strong>and</strong> generate the HTML list of results<br />

for (var i=0; i

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

Saved successfully!

Ooh no, something went wrong!