Autocomplete text box
Moderator: General Moderators
-
TucsonTechie
- Forum Newbie
- Posts: 4
- Joined: Tue Mar 02, 2004 1:14 pm
Autocomplete text box
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.
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.
- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
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':
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)
to find all last names that start with 'ta':
Code: Select all
SELECT * FROM table WHERE last_name LIKE 'ta%'(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
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.
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
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!
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!
- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
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:
You'll need to study some javascript to figure out the rest
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>";-
TucsonTechie
- Forum Newbie
- Posts: 4
- Joined: Tue Mar 02, 2004 1:14 pm
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.
- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
something like this:
Code: Select all
<input type='text' name='some_name' onKeyUp='autocomplete()' />