Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merging PHP and Javascript 1

Status
Not open for further replies.

dodgyone

Technical User
Joined
Jan 26, 2001
Messages
431
Location
GB
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>

================================
&quot;Chaos, panic & disorder - my work here is done!&quot;
================================
 
have you checked the source coude of what you see in the browser so you can see if the javascript code was generated the way you need it to? if so, and all's well, post the result so we can understand the problem better. if not, then explain a bit what is going wrong.
 
The following code is created from the PHP. So the user selects from the first drop-down box and then the relevant list is created in the second drop-down box. The problem is that 'SCFHNT9' appears along with 'lime' and 'Orange' when you switch between the two lists and view the result.

<html>
<head>

<script language="Javascript">

function fillState(form) {
if (form.country.selectedIndex == "1") {
form.state.options[1] = new Option("SCFHNT5", "1");
form.state.options[2] = new Option("SCFHNT8", "2");
form.state.options[3] = new Option("SCFHNT9", "3");
}
else if (form.country.selectedIndex == "2") {
form.state.options[1] = new Option("Lime", "1");
form.state.options[2] = new Option("Orange", "2");
}
}
</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>

================================
&quot;Chaos, panic & disorder - my work here is done!&quot;
================================
 
alot better now...

add this line
Code:
form.state.options.length = 0;
as the first line after if and after else
 
Many thanks Daz, that solved the problem. Is it possible to place a "-- choose --" message or whatever so that the size of the drop-down box doesn't change? I tried...

echo "form.state.options.length = 0;\n";
echo "form.state.options[0] = new Option(\"-- choose --\", \"0\"";

... after the if and else if statements but the second line stops the whole list appearing.

================================
&quot;Chaos, panic & disorder - my work here is done!&quot;
================================
 
echo "form.state.options[0] = new Option(\"-- choose --\", \"0\");

you missed a ')' there and added an extra '"'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top