...

View Full Version : Real time listbox update



rschoenbach
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.

Thanks

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

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

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

aedrin
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.

JohnDubya
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.

http://www.petenelson.com/aspwatch/ASPWatch%20%20Using%20Javascript%20to%20Dynamically%20Populate%20Select%20Lists.htm

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!

CFMaBiSmAd
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum