Alright, what I have is 2 select boxes, in the first you select a value and it updates the second. The data from the first is taken from the database and put into arrays the same as the second. I didn't design the code I am using.
Database Strucutre:
bands_tbl:
-id
-name
albums_tbl
-id
-band_id
-albumname
I have two examples:
http://www.newfoundpunk.com/banddatabase/selectbox4.php - this one works although if you view the source and compare with the source in the second there's no difference
http://www.newfoundpunk.com/banddatabase/selectbox2.php - this doesn't work
I'd be greatful if someone could help as I have been having trouble with this for the last week. Thanx
Here's the code (I've shown it all as you might need it but only the top 30 lines are important for inserted the data into the arrays):
<?
require ("../config.inc.php");
$db = mysql_connect($host,$login,$pass);
mysql_select_db($base,$db);
$sql = mysql_query ("SELECT distinct bdb.* from bands_tbl bdb, albums_tbl adb WHERE bdb.id=adb.band_id");
echo "<script>\n";
echo "var mmMk = new Array();\n";
echo "var mmMd = new Array();\n";
while ($row = mysql_fetch_array($sql)) {
echo "mmMk[".$row["id"]."] = '".$row["name"]."#".$row["name"]."';\n";
}
$sql2 = mysql_query ("SELECT distinct bdb.* from bands_tbl bdb, albums_tbl adb WHERE bdb.id=adb.band_id");
while ($row2 = mysql_fetch_array($sql2)) {
$band_id = $row2["id"];
$name = $row2["name"];
$sql3 = mysql_query ("SELECT * FROM albums_tbl where band_id = '$band_id'");
echo"mmMd['$name'] = '";
while ($row3 = mysql_fetch_array($sql3)) {
echo " ".$row3["albumname"]."#".$row3["albumname"].",";
}
echo"';\n";
}
echo"</script>\n";
?>
<script>
function mmInitialize(parent, category, sub, make_caption, model_caption, bnew)
{
var rgMake;
clearList(parent.mmCategory);
addElement(parent.mmCategory, make_caption, 0);
if (bnew == 'true')
{
for (var i = 0; i < mmMk.length; i++)
{
if (mmMk[i])
{
rgMake = mmMk[i].split('#');
addElement(parent.mmCategory, rgMake[0], rgMake[1]);
}
}
}
else
{
for (var i = 0; i < mmMk.length; i++)
if (mmMkU[i])
{
rgMake = mmMk[i].split('#');
addElement(parent.mmCategory, rgMake[0], rgMake[1]);
}
}
if (category)
{
setDefaultByText(parent.mmCategory, sub);
mmChangeMake(parent, bnew, model_caption);
if (sub)
setDefaultByText(parent.mmSubs, sub);
}
else
{
parent.mmCategory.selectedIndex = 0;
mmChangeMake(parent, bnew, model_caption);
}
}
function mmChangeMake(parent, bnew, model_caption)
{
if (bnew == 'true')
var ModelList = mmMd[parent.mmCategory.options[parent.mmCategory.selectedIndex].value];
else
var ModelList = mmMd[parent.mmCategory.options[parent.mmCategory.selectedIndex].value];
clearList(parent.mmSubs);
addElement(parent.mmSubs, model_caption, 0);
if(ModelList)
{
var rgSubs = ModelList.split(',');
for (var i = 0; i < rgSubs.length; i++)
{
if (rgSubs[i])
{
var rgModel = rgSubs[i].split('#');
addElement(parent.mmSubs, rgModel[0], rgModel[1]);
}
}
parent.mmSubs.disabled = false;
}
else
{
parent.mmSubs.disabled = true;
}
parent.mmSubs.selectedIndex = 0;
}
</script>
<script>
/*
REM JavaScript functions for client-side manipulation of HTML listboxes.
REM Works on both NetScape (4.0+) and IE (4.0+).
REM Look at \source\misc\ymmbuild.asp for an example of how to use these functions.
*/
function clearList(list)
{
var i = 0;
var o = list.options;
for (i = o.length; i >= 0; --i)
o[i] = null;
list.disabled = true;
}
function addElement(list, text_in, value_in)
{
var o = list.options;
var nIdx;
if (o.length < 0) //IE for Mac 4.5 sets length to -1 if list is empty
nIdx = 0;
else
nIdx = o.length;
o[nIdx] = new Option(text_in, value_in);
list.disabled = false;
}
function setDefaultByText(list, text_in)
{
with (list)
{
for (var i = 0; i < (options.length); i++)
{
if (options[i].text == text_in)
{
selectedIndex = i;
return;
}
}
}
}
function setDefaultByValue(list, value_in)
{
with (list)
{
for (var i = 0; i < (options.length); i++)
{
if (options[i].value == value_in)
{
selectedIndex = i;
return;
}
}
}
}
</script>
<!---Body--->
<?
if ($_REQUEST[submit]){
echo"You piked:<br>";
echo"<br>Category: ".$_REQUEST['mmCategory']." ";
echo"<br>Sub: ".$_REQUEST['mmSubs']." ";
exit;
}
?>
<form name="Form2" id="Form2" action="<?= $_SERVER[SCRIPT_NAME] ?>">
<table>
<tr bgcolor="#99ccff">
<td>
<select name="mmCategory" disabled onChange="mmChangeMake(document.Form2, 'true', 'Sub');">
<option value="0">Category</option>
</select>
</td>
</tr>
<tr bgcolor="#99ccff">
<td>
<select name="mmSubs" disabled>
<option value="0">Sub</option>
</select>
</td>
</tr>
</table>
<input type=submit name=submit value=push>
</form>
<script>
mmInitialize(document.Form2, '', '', 'Category', 'Sub', 'true');
</script>