The following code links into a database and retrieves two sets of records. Using JavaScript these two retrieved sets of records are then placed into a dynamic drop-down box when the user selects from another drop-down box. The problem is that a record from the first set of retrieved database records appears in the second list if you switch between the sets of record. Is there any way to stop this happening? I hope you can follow this alright... thanks in advance for any help given:
<html>
<head>
<?php
// connect to the mysql database
include ("database.inc");
// ==========================
// create the javascript code
//===========================
echo "<script language=\"Javascript\">\n";
echo "\nfunction fillState(form) {\n";
// ====================================================================
echo "if (form.country.selectedIndex == \"1\") {\n";
// create the array for servers
$query = "SELECT name FROM server";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
$j = 1;
for ($i=0; $i<$num_results; $i++) {
while ($row = mysql_fetch_array($result)){
echo "form.state.options[$j] = new Option(\"$row[$i]\", \"$j\");\n";
$j = $j + 1;
}
}
echo "}\n";
// ====================================================================
echo "else if (form.country.selectedIndex == \"2\") {\n";
// create the array for coms
$query2 = "SELECT name FROM coms";
$result2 = mysql_query($query2);
$num_results2 = mysql_num_rows($result2);
$k = 1;
for ($i=0; $i<$num_results2; $i++) {
while ($row2 = mysql_fetch_array($result2)){
echo "form.state.options[$k] = new Option(\"$row2[$i]\", \"$k\");\n";
$k = $k + 1;
}
}
echo "}\n";
// ====================================================================
echo "}\n";
echo "</script>";
?>
</head>
<body>
<form name="myForm" id="myForm">
<select name="country" id="country" onChange="fillState(this.form);">
<option value="0"> -- choose one -- </option>
<option value="1">Server</option>
<option value="2">Coms</option>
</select>
<select name="state" id="state">
<option value="0"> -- choose one -- </option>
</select>
<BR><BR>
</form>
</body>
</html>
================================
"Chaos, panic & disorder - my work here is done!"
================================
<html>
<head>
<?php
// connect to the mysql database
include ("database.inc");
// ==========================
// create the javascript code
//===========================
echo "<script language=\"Javascript\">\n";
echo "\nfunction fillState(form) {\n";
// ====================================================================
echo "if (form.country.selectedIndex == \"1\") {\n";
// create the array for servers
$query = "SELECT name FROM server";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
$j = 1;
for ($i=0; $i<$num_results; $i++) {
while ($row = mysql_fetch_array($result)){
echo "form.state.options[$j] = new Option(\"$row[$i]\", \"$j\");\n";
$j = $j + 1;
}
}
echo "}\n";
// ====================================================================
echo "else if (form.country.selectedIndex == \"2\") {\n";
// create the array for coms
$query2 = "SELECT name FROM coms";
$result2 = mysql_query($query2);
$num_results2 = mysql_num_rows($result2);
$k = 1;
for ($i=0; $i<$num_results2; $i++) {
while ($row2 = mysql_fetch_array($result2)){
echo "form.state.options[$k] = new Option(\"$row2[$i]\", \"$k\");\n";
$k = $k + 1;
}
}
echo "}\n";
// ====================================================================
echo "}\n";
echo "</script>";
?>
</head>
<body>
<form name="myForm" id="myForm">
<select name="country" id="country" onChange="fillState(this.form);">
<option value="0"> -- choose one -- </option>
<option value="1">Server</option>
<option value="2">Coms</option>
</select>
<select name="state" id="state">
<option value="0"> -- choose one -- </option>
</select>
<BR><BR>
</form>
</body>
</html>
================================
"Chaos, panic & disorder - my work here is done!"
================================