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 6visible, we need to scroll in the region in order to make that result visible. In order to implementthis, we keep minimum <strong>and</strong> maximum positions of the results that are currently visible. It's as ifwe had a window that moves through the results according to the arrows' movements <strong>and</strong> thecurrent selected result.The selectRange <strong>and</strong> autocompleteKeyword functions do the trick for the type-ahead look byautocompleting the current keyword with the rest of the missing letters up to the first suggestion.The part that is missing is added as highlighted text to the current keyword. The select() methodselects all the text, <strong>and</strong> hence selecting only a part of a text is not possible. In order to do this,Internet Explorer offers one solution while Mozilla / Firefox offers another one. It is not for thefirst time that issues are not the same in all browsers, so we have to take each case <strong>and</strong> solve itseparately. In Firefox, issues are simple because there is just one function that does all the workfor us—setSelectionRange. This function takes two parameters—the start position of theselection <strong>and</strong> the length of the selection. In Internet Explorer, we have to use the TextRange objectin order to achieve the same goal. Let us take a closer look at it because it might be useful for us inthe future <strong>and</strong> for this, we need to know what it can do.The TextRange object can carry out tasks such as searching or selecting text. Text ranges let youpick out characters, words, <strong>and</strong> sentences from the text. Each of these three is a logical unit of theobject. In order to use such an object you have to follow these steps:• Create the text range• Apply a method to the selected textYou can copy the text, search in the text, <strong>and</strong> select a part of the text, as in our case.To create such an object you can call the createTextRange method on a body, textarea, orbutton element.Each object has a start <strong>and</strong> an end position defining the scope of the text. When you create a newtext range, the start <strong>and</strong> end positions contain the entire content by default. To modify the scope ofthe text range we can use the move, moveStart, <strong>and</strong> moveEnd functions. Each of them takes twoparameters—the first parameter specifies the logical unit <strong>and</strong> the second one the number of unitsto move. The result contains the numbers of units moved. The select method makes the selectionequal to the current object. In order to have a complete view of its capabilities check the followinglink on MSDN: http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dyncontent/textrange.asp.After receiving the suggestions <strong>and</strong> inserting them into the page, we need to autocomplete thekeyword with the value of the first suggestion. This is accomplished by using the selectRangefunction described above.For the error-h<strong>and</strong>ling part, we use the displayError function that displays an alert with the errormessage as parameter.OK, now we have seen how it goes for the client side of the application. Let's check the server side.For the server side, things are very simple. The suggest.php file retrieves the parameter passed bythe client <strong>and</strong> that represents the searched for keyword. Then it calls a method of the Suggest class187

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

Saved successfully!

Ooh no, something went wrong!