Autocomplete text box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TucsonTechie
Forum Newbie
Posts: 4
Joined: Tue Mar 02, 2004 1:14 pm

Autocomplete text box

Post by TucsonTechie »

I have a mySQL table that houses an address book. (fields : firstname, lastname, address, phone, etc)

I have a PHP script that contains an HTML form.
On the form is a lastname field. As I start typing into the lastname form field, I would like it to autocomplete my entry with the contents of the lastname field from the table.

Is this only possible with client-side coding, such as javascript?

I am relatively new to PHP, but not clueless. Please give me some direction.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

That's only possible with client-side code and not really possible the way you describe it. But you can make it so that when you submit something incomplete it will show all the matches:

to find all last names that start with 'ta':

Code: Select all

SELECT * FROM table WHERE last_name LIKE 'ta%'
but that requires you to submit the incomplete name first.

(actually it might be possible to do something like that using flash or a java applet)
TucsonTechie
Forum Newbie
Posts: 4
Joined: Tue Mar 02, 2004 1:14 pm

Post by TucsonTechie »

Can you explain how they did it here...

http://www.somacon.com/phpref/

here is the HTML code for this page http://www.somacon.com/phpref/widgetpage.php

You can see the javascript being used. The entries are hardcoded in the javascript...

functionlist = Array("abs", "acos", "acosh", ...

... but perhaps there is a way to populate that list with the table data? I suppose now is the time for me to start studying javascript-->mySQL..

Here is another example of an autocomplete box...
http://uk.php.net/search.php
I imagine they used javascript + php also.

Thanks for your help.
TucsonTechie
Forum Newbie
Posts: 4
Joined: Tue Mar 02, 2004 1:14 pm

Post by TucsonTechie »

A ha!
I can create the javascript dynamically with PHP...so I can have PHP rewrite the following...

var functionlist = Array("abs", "acos", "acosh", "addcslashes"....

...each time the page loads.

So I just need to do a query for unique names and then re-write that javascript array definition with the values from the query.

If anyone has suggestions for how to write the php to do that, it would save me some time. Thanks!
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

Sorry, I guess i didn't think of that (even though i have seen it on php.net). Writing javascript using php is exactly the same as writing html with php. The code might look something like this:

Code: Select all

<?
$query = mysql_query("SELECT * FROM table_name") or die(mysql_error());

echo "<script>\n";
echo "var functionlist= Array(";

$counter=0;
while ($item = mysql_fetch_array($query)) {
    if ($counter != 0) echo ','; $counter++;         //add coma if not first
    echo ""$item['last_name']"";
}

echo ");\n";
echo "</script>";
You'll need to study some javascript to figure out the rest
TucsonTechie
Forum Newbie
Posts: 4
Joined: Tue Mar 02, 2004 1:14 pm

Post by TucsonTechie »

Excellent! That is extremely helpful. Thank you very much :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You could do a SQL query to get all unique values, and put that in a Javascript array. Then using a Javascript listener (onChange comes to mind - something that will trigger after every key stroke), have the values in the textbox be pulled from the Javascript array and displayed in the textarea below.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

something like this:

Code: Select all

<input type='text' name='some_name' onKeyUp='autocomplete()' />
Post Reply