On Tuesday, June 25, 2002, at 04:01 PM, Phillip S. Baker wrote:
> Yes I knew it was Javascript code, but I also thought PHP was needed to > get the values from the DB. > Either case I do need help with what the code might look like. Okay, here it is... kinda complicated, but using this theory you should be able to do it: You need to populate an HTML listbox based on DB data. Make sure your SELECT query gets both the primary key of each record and the actual name that you want to appear in the listbox. Dynamically generate the listbox in the normal way, by using the primary key as the "value" attribute of each <option> tag and using the other column value as the CDATA contents of the option element, like this: <?php $sql = "SELECT id, name FROM table"; $result = mysql_query($sql, $db); $output = ''; while ($row = mysql_fetch_assoc($result)) { $output .= "\t<option value=\"" . $row['id'] . "\" onselect\"controlOther(" . $row['id'] . ")\">" . $row['name'] . "</option>\n"; } ?> You will have something like this in $output: <option value="1" onselect="controlOther(1)">Yoda</option> <option value="2" onselect="controlOther(2)">Ben</option> <option value="3" onselect="controlOther(3)">Annikin</option> <option value="4" onselect="controlOther(4)">Luke</option> The "controlOther()" is a JavaScript function that you will write in a moment. Anyway, you now have a listbox with values that exactly match your JavaScript array. What you do next is you assign a name to the select tag which holds the listbox values, and place the listbox values into the select tag: <?php $selectTag = "<select name='jedimasters'>" . $output . "</select>\n"; ?> And of course you need to put your select tag into a named form (the names are important for the JavaScript!): <?php $form = " <form method=\"" . $_SERVER['PHP_SELF'] . "\" name=\"choosejedi\"> BLAH BLAH BLAH other inputs can go here " . $selectTag . " </form>"; ?> Okay! Now you have your form, named "choosejedi", your input, named "jedimasters", and a listbox filled with name/value pairs. Your intended effect is to allow one listbox's actions to control another listbox's data. For this, you need to write a JavaScript function that controls the other listbox. <?php $coFunc = " <script type='text/javascript'> <!-- function controlOther(choice) { var contents = ''; switch(choice) { case 1: contents = AAAA; break; case 2: contents = BBBB; break; case 3: contents = CCCC; break; case 4: contents = DDDD; break; default: alert('Invalid value selected'); } // end switch() --> </script>\n"; ?> You can see what this function does -- it takes the value of the argument passed to it and executes certain other code according to the switch() statement. I don't have time to actually write out possible code for these cases, so I just put "AAAA" or "BBBB" etc (it's all a hypothetical example anyway). You will want to have already generated this data from the DB using PHP anyway, so you would just plug in a few variables here (whose values are defined earlier in the script). The point is that every time someone makes a selection in the listbox, they will trigger the ONSELECT event handler, which calls the "controlOther()" JavaScript function. The value passed to controlOther() tells ANOTHER listbox on the page what it should be populated with, based on the switch() statement. You need to have names for all your forms and your inputs so that you can refer to them using the JavaScript Document Object Model... in other words, your controlling code would look something like document.choosejedis.jedimasters.value refers to the selected value in the main listbox, and can be used to perform actions on other listboxes. ANYWAY... this got way longer than I intended. Basically, I haven't tested any of the above but in theory it should work, but be sure to do mad browser detection so you can be sure that it'll work, etc. I personally wouldn't even bother, this kind of coding is pretty technical and you may as well just do it in PHP in case the user doesn't have JS turned on. I think it's a lot easier to do it in PHP too. Sure, the user will have to submit the page to update the new listbox, but... not so hard, eh? Essentially it boils down to this pseudocode: 1. use PHP to dynamically generate a main listbox to control the next listbox 2. use PHP to dynamically generate possible values for the secondary listbox (I would just throw 'em in arrays) 3. determine which array should populate the second listbox based on the input in the first listbox using JavaScript event handlers in the first listbox HTH, Erik ---- Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php