AutoCompleter Tutorial
As always, links to a demo and ZIP at the bottom, Enjoy!
I thought i would write this tutorial because most of the auto completer applications i have seen just dump the code into a zip and tell you how to use it rather than how and why it works, knowing about this enables you to customise it a lot more (this has been demonstrated with the other apps i have written here)!

And so we begin:
JavaScript
<script src="jquery-1.2.1.pack.js" type="text/javascript"><!--mce:0--></script><script type="text/javascript"><!--mce:1--></script>
JS Explaniation
Ok, the above code it the guts to the script, this allows us to hook into the rpc.php file which carries out all the processing.
The Lookup function takes the word from the input box and POSTS it to the rpc.php page using the jQuery Ajax POST call.
IF the inputString is ’0′ (Zero), it hides the suggestion box, naturally you would not want this showing if there is nothing in the text box. to search for.
ELSE we take the ‘inputString’ and post it to the rpc.php page, the jQuery $.post() function is used as follows…
$.post(url, [data], [callback])
The ‘callback’ part allows to hook in a function, this is where the real magic if thats what you can call it happens.
IF the ‘data’ returned length is more than zero (ie: there is actually something to show), show the suggestionBox and replace the HTML inside with the returned data.
SIMPLE!
Now for the PHP Backend (RPC.php)
As always my PHP Backend page is called rpc.php (Remote Procedure Call) not that it actually does that, but hey-ho.
// PHP5 Implementation - uses MySQLi. $db = new mysqli('localhost', 'root' ,'', 'autoComplete'); if(!$db) { // Show error if we cannot connect. echo 'ERROR: Could not connect to the database.'; } else { // Is there a posted query string? if(isset($_POST['queryString'])) { $queryString = $_POST['queryString']; // Is the string length greater than 0? if(strlen($queryString) >0) { // Run the query: We use LIKE '$queryString%' // The percentage sign is a wild-card, in my example of countries it works like this... // $queryString = 'Uni'; // Returned data = 'United States, United Kindom'; $query = $db->query("SELECT value FROM countries WHERE value LIKE '$queryString%' LIMIT 10"); if($query) { // While there are results loop through them - fetching an Object (i like PHP5 btw!). while ($result = $query ->fetch_object()) { // Format the results, im using <li> for the list, you can change it. // The onClick function fills the textbox with the result. echo '</li> <li onclick="fill(''.$result->value.'');">'.$result->value.'</li> '; } } else { echo 'ERROR: There was a problem with the query.'; } } else { // Dont do anything. } // There is a queryString. } else { echo 'There should be no direct access to this script!'; } } ?>
PHP Explaination
Im not going to go into much detail here because there are loads of comments in the code above.
Basically, it takes the ‘QueryString’ and does a query with a wildcard at the en.
This means that in this case of the code each key-press generates a new query, this is MySQL intensive if its always being done, but short of making it exceedingly complex it is fine for small scale applications.
The PHP code is the part you have to change to work in your system, and all the it is updating the ‘$query’ to your database, you should be able to see where you put the table column name in etc…
CSS Styling – im using CSS3, YEA BABY! much easier although limits the functionality to Firefox and Safari.
<!-- .suggestionsBox { position: relative; left: 30px; margin: 10px 0px 0px 0px; width: 200px; background-color: #212427; -moz-border-radius: 7px; -webkit-border-radius: 7px; border: 2px solid #000; color: #fff; } .suggestionList { margin: 0px; padding: 0px; } .suggestionList li { margin: 0px 0px 3px 0px; padding: 3px; cursor: pointer; } .suggestionList li:hover { background-color: #659CD8; } -->
The CSS is pretty standard, nothing out of the ordinary.
Main HTML
<div>
<div>
Type your county (for the demo):
<input id="inputString" onkeyup="lookup(this.value);" size="30" type="text" />
</div> <div class="suggestionsBox" id="suggestions" style="display: none;">
<img style="position: relative; top: -12px; left: 30px;" src="upArrow.png" alt="upArrow" />
<div class="suggestionList" id="autoSuggestionsList">
</div>
</div>
</div>That is the main bit of HTML, really all you need to run this is an input text box with the ‘onkeyup’ function set to lookup(this.value); Also, i recommend NOT changing the ID of the input box, unless you change it in the Javascript section
Screenshots
I suppose you want to see some more screen shots…. OK.

And another one!

And now for the links… this is probably what you are all looking for!
Demo: Auto Complete Demo
Source ZIP: AutoComplete Source ZIP
If you need a domain to get you off your local test box buy domains at Network Solutions!
If you have any problems, just post them up here (as comments) and i am sure i can help you fix it
Thanks for the continued support,
Jamie.

Hi
Could we use this software with arabic character?
Thanks in advance for your answer?
Regards
Abder
I used your software in :
http://www.unionartistes.com/umda/main/test.php?langue=fr
when I use langue=fr,
we can see a list
but when I use langue=ar, this list is empty
could you help me please?
Regards
[...] Autocomplete by AjaxDaddy jQuery Autocomplete Plugin with HTML formatting jQuery Autocompleter AutoCompleter (Tutorial with PHP&MySQL) quick Search jQuery [...]
your answer is not clear
tks
Thank you very much for the code:)
I also have a question about the chars, cause I always get strange symbols, by having these letters “ä”, “ö” and “ü”
Do you have a solution so far?
Thank you very much!
thomas
hello, i’m trying to pass another input from a drop down box to use in the where part of the sql but i cant seem to get it to work.
any ideas?
No compatible jquery-ui ? I have problem only by add javascript file in head.
I think the problem can come from a similair function name ? or by the compilation similary.
hi,
i have tried using the code. it searches for the results from the database but in the results i can only see the bullets but not the text. can you please help me with that.
[...] the code is at http://www.nodstrum.com/2007/09/19/autocompleter/comment-page-26/#comment-305141 [...]
[...] Autocomplete by AjaxDaddy. jQuery Autocomplete Plugin with HTML formatting. jQuery Autocompleter. AutoCompleter (Tutorial with PHP&MySQL). quick Search jQuery [...]
mohit,
You could be seeing the bullets because it is picking up results but you are not echoing the items you want to see. Try changing the “value” to the name of the column you want to show in this area:
;value(change “value” to column name in database).”);”>’.$result->;value(change “value” to column name in database).’
Hope this fixes it!
i want use auto suggest two times in different text fields in my project,i am passing two different query from two different php file,result also display from autosuggest,but
output text goes in only one text field.how we write two differnt javascript functions for two differnt text fields.
Hi,
I’m looking for a sample with a form containing 2 input fields that would be autocompleted from 2 different tables.
Anyone could send some piece of code ?
??????? ????? ??????? ??????!
?????? ? ???? ?? ????????, ?????? ?? ?????????? ?????,???? ??? ???????? ?????????.
???????? ?????????? ??? ????? ????? ???? ??????
Thanks very good work!
I just did not work, or rather does not show the text, although it has registered correctly.
Help please I really need this script
@ Assassin
Look at Scott’s last post today.
’.$result->;value(change “value” to column name in database).’
Thank you! Just do not understand how to limit the output of 10 pieces? do not know how?
Using a LIMIT statement in your SQL should work: http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
SELECT * FROM `your_table` LIMIT 0, 10
hello,
this is very usedful,but there may some extra code needed
Nice example, will be downloading and running it on my local computer to see if it works.
Great job.
Bravo…!!!! Wonderful Code…!!! was looking for this….i have a doubt…..u said its Mysql intensive….can u tell me the reason???
Hello,
I wanted to convert the script to mysql. How do i do that?
Regards,
Mihir.
how do I make it evaluate which value I’m typing and remove the values that do not match b/c the suggest I have right now from what you gave me in the .ZIP, you go to type say A and it lists all values starting with A but when you start to type more like ALLY it doesnt remove the values that do not match my input.
Hi
i want to use this on a div, for making an IDE. can you please give an example how to do it when i keyin a particular keys
Thanks
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
Used it in host and it works great, but when I uploaded it to a host, I get this instead.
Fatal error: Class ‘mysqli’ not found in C:\hshome\spurgeon\spurgeonschool.edu.ph\pn\references\rpc.php on line 8
I checked my mysql connections and it is perfect. Can anyone please help? Thanks.
Hey I love this tutorial, it’s amazing! How would I set this up if I wanted to do to auto-complete fields? (ie. city and state). Thanks a lot for your help!
[...] 81. AutoCompleter [...]
[...] Autocomplete by AjaxDaddy jQuery Autocomplete Plugin with HTML formatting jQuery Autocompleter AutoCompleter (Tutorial with PHP&MySQL) quick Search jQuery [...]
Hi, is this compatible with IE and Chrome?
It seems not or I have made some mistakes, in Firefox it sees to run…
Regards
Got the script working fine with my database.
When I choose the option in the drop-down menu I would like to then automatically submit the content of the text field.
Can you tell me how I could do this please?
Thanks
nice work
[...] Autocomplete by AjaxDaddy. jQuery Autocomplete Plugin with HTML formatting. jQuery Autocompleter. AutoCompleter (Tutorial with PHP&MySQL). quick Search jQuery [...]
thank you sir,
but when i select the value it is not displaying values on the field
[...] Autocomplete by AjaxDaddy. jQuery Autocomplete Plugin with HTML formatting. jQuery Autocompleter. AutoCompleter (Tutorial with PHP&MySQL). quick Search jQuery [...]
Good script and it does exactly what I want. However, the one thing it doesn’t do is save the value within the textbox when I go to submit the form. I’ve even tested your demo and it does just the same. Funny thing as inclusion within a form is pretty much the only use for this.
Anyone sorted this? It’s winding me up.
nice
Got this working fine… but now trying to use multiple autocompletes for various fields.. and don’t seem to be able to figure it out…
My bad, I didn’t assign a name to the field that was being passed to the script. HERP-DE-DERP.
[...] Demo | Source page [...]
[...] the code is at http://www.nodstrum.com/2007/09/19/autocompleter/comment-page-26/#comment-305141 [...]
HI,
I was able to implement this!! thanks..
but I have some issue. on selection fron list I want to popolate data in the text box again? can anybody enhance the code.
also some symbols are getting added at run time
“”
Can this b avoided??
Regards, Nalini
[...] Autocomplete by AjaxDaddy. jQuery Autocomplete Plugin with HTML formatting. jQuery Autocompleter. AutoCompleter (Tutorial with PHP&MySQL). quick Search jQuery [...]
[...] Autocomplete by AjaxDaddy. jQuery Autocomplete Plugin with HTML formatting. jQuery Autocompleter. AutoCompleter (Tutorial with PHP&MySQL). quick Search jQuery [...]
[...] AutoCompleter (Tutorial with PHP&MySQL). [...]
So far this script is GREAT ! I would like to use it as an auto completer for my website search…
Is there a way to make the form automatically submit when you click the selection menu.
When you click your selection I would like it to fill the form and auto submit.
Thanks
[...] AutoCompleter [...]
[...] http://www.nodstrum.com/2007/09/19/autocompleter/ – Tutorial on the Autocomplete – jQuery (Ajax)/PHP/MySQL [...]
Thanks for the post. Following worked perfectly for me.
I modifed rpc.php to get results off of shell script. Example:
$query=explode(“\n”,shell_exec(“bash /cygdrive/c/scripts_dir/myscript.sh arg1 $queryString”));
$query=array_filter($query);
if(count($query)>0) {
foreach($query as $index => $result){
$result=addslashes($result);
echo ”.$result.”;
I had to use addslashes($result) to handle names with single quote, for example, O’neill.
The divs code did not get along with table tags (autocomplete did not work), so I made divs only session page for one page and tables for the rest.