View Full Version : Real time listbox update

03-14-2007, 08:16 AM
I have one listbox that I populate with

while($row = mysql_fetch_array($result, MYSQL_ASSOC)

I need to in real time populate another listbox from a related table based on what they have currently selected in the first listbox.

Is this possible with php and if so.. some pointers would be nice.


03-14-2007, 08:56 AM
I think with some help of javascript this will be possible, although, no pointers here.

03-14-2007, 01:53 PM
Refresh the page every second.

Tip: Create a JavaScript so you dont get a flicker!

03-14-2007, 03:22 PM
Since you mention realtime, you'd have to use AJAX here.

Otherwise you'd have to submit the page every time the dropdown changes.

03-14-2007, 05:05 PM
rschoenbach, here's a link to a site that describes how to dynamically fill select boxes with pre-fetched values.


You can use mysql_query(), mysql_fetch_array(), and while() loops to grab the values that need to fill the select boxes. It's a bit complicated at first, but once you stare at it for hours like I did, it will become clear...hopefully. :D If you need help integrating the PHP code, let me know, and I'll try to help. I can at least show you how I did it.

[EDIT] Just to give you a start, I'll give you a hint of how to fill up the select box. So if your first select box (SportsList) equals 1:

if (SportsList[SportsList.selectedIndex].value == "1") {
AddToOptionList(document.frmMain.Teams, "1", "Oakland Athletics");
AddToOptionList(document.frmMain.Teams, "2", "San Francisco Giants");

It puts in the two options above. But you can just use PHP/MySQL to grab info from your DB and throw the info into these AddToOptionList entries:

if (SportsList[SportsList.selectedIndex].value == "1") {

$x = mysql_query("SELECT id, name FROM teams WHERE category_id = '1'");

while ($y = mysql_fetch_array($x)) {
$id = $y['id'];
$name = $y['name'];

echo 'AddToOptionList(document.frmMain.Teams, "' . $id . '", "'. $name .'");

That should at least get you started. Cheers!

03-14-2007, 05:12 PM
Here is a current thread on how to do this using AJAX - http://www.codingforums.com/showthread.php?t=108526