Jump to content

Archived

This topic is now archived and is closed to further replies.

digitalgod

dynamic drop down

Recommended Posts

hey guys,

I have 2 drop downs, my first one is being populated by an array but I'd like to have my 2nd one populated from a db depending on the value of the first drop down.

I already know how to do it but my way requires the form to be refreshed... would there be a simple way to do is without having to reload the page?

Share this post


Link to post
Share on other sites
I know and I tried different solutions but without any success....

here's what I have so far
[code=php:0]
echo '<SCRIPT language=JavaScript type=text/javascript> ';
echo 'var store = new Array();';
for ($i=0; $i<$numRows; $i++){ //for each item
$js_array = "";
$sql = 'SELECT * FROM clubnights';

$result = mysql_query($sql) or die (mysql_errno().": select ".mysql_error()."<BR>" . $sql);
while ($row = mysql_fetch_array($result)) {
$js_array .= "'" . $row['club'] . "','" . $row['club'] . "',";
}
$js_array = rtrim( $js_array , ","); // trim final comma
echo 'store['.$i.'] = new Array('; // start javascript array
echo $js_array . "); ";
} // end for
echo '</SCRIPT>';
[/code]
[code]
<SCRIPT language=JavaScript type=text/javascript>
<!-- Begin

/* Execute init() function */
window.onload = initialize;
function initialize () {
if (self.init) self.init();
}

function init()
{
optionTest = true;
lgth = document.forms[0].night.options.length - 1;
document.forms[0].night.options[lgth] = null;
if (document.forms[0].night.options[lgth]) optionTest = false;
if (optionTest) self.populate();
}

function populate()
{
if (!optionTest) return;
var box = document.forms[0].club;
var number = box.options[box.selectedIndex].value;
if (!number) return;
var list = store[number];
var box2 = document.forms[0].night;
box2.options.length = 0;
for(i=0;i<list.length;i+=2)
{
box2.options[i/2] = new Option(list[i],list[i+1]);
}
[/code]

where "night" is m first drop down and "club" is my second.

night is populated by an array and I want "club" to be populated depending on what night is chosen

my table clubnights looks like this

id  | night      | club  |.....
1  | Friday     | Aria   |
2  | Thursday | Circus |
..... etc

so a night can have several clubs

I'm really lost

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.