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.

Chapter 6We have talked about caching the results. Yes, this is a very good optimization technique for thiskind of application. Therefore, we have two functions that deal with the cache object—checkCache <strong>and</strong> addToCache.The checkCache function checks to see if a given keyword is in the cache. If it's not in the cache, ittries to find the longest matching prefixes for our keyword in the list of cached values. Thosematching prefixes are added to the cache by calling the addToCache function.The addToCache function inserts in the cache for a given keyword a list of values that representthe suggestions for the keyword.The getSuggestions function is called for fetching new suggestions. If the current keyword isalready in the cache (checkCache function), we populate the suggestions list directly with thosesuggestions that have been cached. If a request is already in progress, the keyword that we wouldhave wanted to use for a new call is saved <strong>and</strong> a timeout for this function is set. This way, wemake sure that we save the last keyword for which we could not make a server call <strong>and</strong> as soon asthe current request completes a new server call is initiated with the last keyword.The h<strong>and</strong>leGettingSuggestions function checks to see when the request to the server iscompleted <strong>and</strong> if there are no errors, the updateSuggestions function is called.The updateSuggestions function checks to see if it is necessary to update the suggestion list. Wecheck to see if during the server call there was another attempt to initiate a server call. By this weknow if the user modified the current keyword <strong>and</strong> if so we don't need to display the retrievedsuggestions since they are no longer interesting for the user. Nevertheless, the client caches all thesuggestions from the server.The xmlToArray function is the one that converts a collection of XML nodes into an array.The function that actually builds the list of suggestions is displayResults. It receives as parametersthe keyword <strong>and</strong> the list of available functions as an array. The first thing to do is to cache the currentresults, so that if we want to search again the same keyword, we don't have to make another call tothe web server. We go through all the suggestions in the array <strong>and</strong> we dynamically build a tablecontaining the suggestions. If no suggestions are available, a message is displayed.The updateKeywordValue function is responsible for updating the current keyword with the valuecontained in the suggestion currently selected given as a tr object.The hideSuggestions function hides the div element containing all suggestions for thecurrent keyword.The deselectAll function deselects the currently selected suggestions.The h<strong>and</strong>leOnMouseOver <strong>and</strong> h<strong>and</strong>leOnMouseOut functions h<strong>and</strong>le the events that occur when themouse cursor enters or exits the tr area of a suggestion. These functions update the style of thesuggestion where the event takes place accordingly.The encode function escapes the string passed as a parameter <strong>and</strong> it is used by thegetSuggestions function when calling the server page.185

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

Saved successfully!

Ooh no, something went wrong!