Google's auto-suggestion box is very sophisticated, and I mention it only to give you the general idea. Today's simple example is much less ambitious. We'll discuss how to set up a text input box that is paired with a hidden listbox that's pre-filled with "suggestions" that have been read from from a server-side database. Then we'll look at some code to handle the U/I events --- and save your users some effort.
To follow along you'll need a PHP web-page and a database such as MySQL to obtain the list of values.
2. Create a div with a width of 25% and set the visibility attribute to 'hidden' and a relatively short
height like around 100-150px and set the id attribute to something relevant like 'divSuggestions'
because we'll be referring to this div quite often via JavaScript's getElementById() function.
That's it! We'll use a little JavaScript to toggle the visibility of the box and some PHP and JavaScript combined to fill the box with relevant values from our database.
Somewhere in the header section we need to create a JavaScript array variable that we'll use a little later. I'm calling this variable 'nameArray' because I'm looking up first and last names for my list of values.
Okay, we now have the HTML essentials as far as the text and suggestion-list boxes are concerned. Now we need to add functionality to these elements. When the user begins typing, we want the suggestions box to display and the user's input to show in the text-box. For instance, when a user types in the letter 's', we want the suggestions box to show all values from our table that begin with the letter 's'.
There are a lot of little things that we need to take into consideration when it comes to functionality. For this super-simple example we're going to display the box whenever a user begins to type. If the user types input to the point where there are not any matching values, the box disappears. The box will also disappear when the user erases all the text in the box (i.e. the text-box is empty).
We will want to 'highlight' the suggestions when the user moves the mouse over them and add an event when the user clicks on a suggestion, the suggestions box disappears and the textbox is filled with the selected suggestion.
Finally we will make the suggestions box disappear when the user clicks anywhere in the page outside of the suggestions box. All these little features makes for a nice, simple suggestions-box familiar to anyone wh's used the Google search box.
Let's get to JavaScript-ing and PHP-ing! Down at the bottom of the page just inside the </body> tag we'll create our javascript section. Inside the javascript tags we'll need to create 4 functions.
We need to create a function that grabs the input text as the user types and determine if the suggestions box should show or not. Remember: 'divSuggestions' is the id of my textbox.
When a user clicks on a suggestion, we want to hide the suggestion box and display the selected suggestion in the text box (the ID of my text box is 'txtSearch').
When a user moves the mouse over a suggestion, we want to highlight it. You can use whatever colors you want - I'm using (background/foreground) purple/white and white/black.
The following function actually creates the suggestions as individual divs to place into the suggestions box.
Okay, we're almost done. All we need to do now is grab the list of values from our database. So at the top of our page just inside the <body> tag we'll add our <?php ?> section. Inside our php section we need to connect to the database and store the list of values in the JavaScript array that we created at the beginning. I called mine 'nameArray' because I'll be getting first and last names from my database.
And the last thing that we need is to set the onclick event for the body so that when the user clicks anywhere on the page outside of the suggestions box, the suggestions box will hide. And when the user types in the box we need to call the doSuggestionBox function - so we need to set the onkeyup event for the text box.
That's it. Now you have a working Google-like suggestions box. My source code is attached. Of course you can add more features and functionality as seen fit.
-cheers
by: pathan_fahad on 2010-08-23 at 22:38:58ID: 18690