Page 1 of 1

Autocomplete text box

Posted: Tue Mar 02, 2004 1:14 pm
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.

Posted: Tue Mar 02, 2004 1:20 pm
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)

Posted: Tue Mar 02, 2004 2:28 pm
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.

Posted: Tue Mar 02, 2004 2:43 pm
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!

Posted: Tue Mar 02, 2004 3:06 pm
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

Posted: Tue Mar 02, 2004 5:39 pm
by TucsonTechie
Excellent! That is extremely helpful. Thank you very much :)

Posted: Tue Mar 02, 2004 6:36 pm
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.

Posted: Tue Mar 02, 2004 6:55 pm
by andre_c
something like this:

Code: Select all

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