For the last two days, I have been butting my head against my monitor trying to send data to a drop-down menu from a JavaScript. Here's the JavaScript:

<script>
function myFunction(str)
{

var SystemNum = str;
alert(SystemNum);

document.getElementById("TechNum").value = SystemNum;
alert(document.getElementById("TechNum").value);

location.reload(true);
}
</script>

And the PHP:

echo '<form name="SystemForm" action="Retrieve.php" method="post" >';
echo '<select name="SystemNum" size="8" multiple="multiple" style="text-align:left;" onChange = "myFunction(this.value)">';
	echo '<option value = "0">All Systems In Texoma</option>';

$result = mysql_query("SELECT * FROM Systems ORDER BY LongSystem");
		while($row = mysql_fetch_array($result));
		{
			$SystemNum = $row['SystemNum'];

			echo '<option value = "' . $SystemNum . '">' . $row['LongSystem'] . '</option>';

		}

echo '</select>';

echo '<div class = "center" style="width:400px; margin:auto">';
	echo '<select name="TechNum" id = "TechNum" size="8" multiple="multiple" style="text-align:left;">';
	echo '<option value="0">All Techs in System</option>';

Because of the alert at the beginning of the function, I know that the value of the selected menu item is being passed correctly to the function. Why then, is the value not being passed to the "document.getElementById()" statement?

There has to be a simple solution that I'm overlooking.

    Firstly, you posted this in the wrong forum. There's a specific forum here for ClientSide Technologies.

    It might be because you are referring to an HTML <select> element's "value" property (and you are also setting it). I'm not certain, but I don't believe any such property exists for a SELECT element.

    If I were you, I would learn how to use jQuery. Browsers tend to have strange quirks which make them behave differently and jQuery not only smooths out these quirks and gives a unified and consistent interface, it is also more expressive and your Javascript tends to be shorter and more readable.

      Correct sneaky. HTML select element has a .selectedIndex property to use in javascript.

        Write a Reply...