Jump to content

dynamic drop down


digitalgod

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?

Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.