AJAX and PHP
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 6<br />
visible, we need to scroll in the region in order to make that result visible. In order to implement<br />
this, we keep minimum <strong>and</strong> maximum positions of the results that are currently visible. It's as if<br />
we had a window that moves through the results according to the arrows' movements <strong>and</strong> the<br />
current selected result.<br />
The selectRange <strong>and</strong> autocompleteKeyword functions do the trick for the type-ahead look by<br />
autocompleting the current keyword with the rest of the missing letters up to the first suggestion.<br />
The part that is missing is added as highlighted text to the current keyword. The select() method<br />
selects all the text, <strong>and</strong> hence selecting only a part of a text is not possible. In order to do this,<br />
Internet Explorer offers one solution while Mozilla / Firefox offers another one. It is not for the<br />
first time that issues are not the same in all browsers, so we have to take each case <strong>and</strong> solve it<br />
separately. In Firefox, issues are simple because there is just one function that does all the work<br />
for us—setSelectionRange. This function takes two parameters—the start position of the<br />
selection <strong>and</strong> the length of the selection. In Internet Explorer, we have to use the TextRange object<br />
in order to achieve the same goal. Let us take a closer look at it because it might be useful for us in<br />
the future <strong>and</strong> for this, we need to know what it can do.<br />
The TextRange object can carry out tasks such as searching or selecting text. Text ranges let you<br />
pick out characters, words, <strong>and</strong> sentences from the text. Each of these three is a logical unit of the<br />
object. In order to use such an object you have to follow these steps:<br />
• Create the text range<br />
• Apply a method to the selected text<br />
You can copy the text, search in the text, <strong>and</strong> select a part of the text, as in our case.<br />
To create such an object you can call the createTextRange method on a body, textarea, or<br />
button element.<br />
Each object has a start <strong>and</strong> an end position defining the scope of the text. When you create a new<br />
text range, the start <strong>and</strong> end positions contain the entire content by default. To modify the scope of<br />
the text range we can use the move, moveStart, <strong>and</strong> moveEnd functions. Each of them takes two<br />
parameters—the first parameter specifies the logical unit <strong>and</strong> the second one the number of units<br />
to move. The result contains the numbers of units moved. The select method makes the selection<br />
equal to the current object. In order to have a complete view of its capabilities check the following<br />
link on MSDN: http://msdn.microsoft.com/library/default.asp?url=/workshop/<br />
author/dyncontent/textrange.asp.<br />
After receiving the suggestions <strong>and</strong> inserting them into the page, we need to autocomplete the<br />
keyword with the value of the first suggestion. This is accomplished by using the selectRange<br />
function described above.<br />
For the error-h<strong>and</strong>ling part, we use the displayError function that displays an alert with the error<br />
message as parameter.<br />
OK, now we have seen how it goes for the client side of the application. Let's check the server side.<br />
For the server side, things are very simple. The suggest.php file retrieves the parameter passed by<br />
the client <strong>and</strong> that represents the searched for keyword. Then it calls a method of the Suggest class<br />
187<br />
www.it-ebooks.info