Derek Hutchinson Derek Hutchinson - 2 months ago 7
HTML Question

Populate Drop Dow List from Drop Down List

EDIT: This first post now contains working code and has the problem edited out of it.

I am making a web page that is populating a DDL from a SQL query. Upon selection it executes a 2nd SQL query to populate the second DDL. The script is firing and no errors are being raised but the 2nd DDL does not populate.

My code

<Head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>$(function() {
$("#exchangenameselect").change(function() {
$("#jobnoselect").load("getter.php?choice=" + $("#exchangenameselect").val());
});
});
</Script>
</Head>
<body>
<!-- Perex -->
<div id="perex" class="box">
<?php $conn = mysqli_connect('host', 'user', 'pass', 'database')
or die ('Cannot connect to db');?>

Exchange:
<p><select id="exchangenameselect">
<option>Choose</option>

<?php
$result = $conn->query("select distinct Exchange from MasterJobTable");
while ($row = $result->fetch_assoc()) {
unset($exchange);
$exchange = $row['Exchange'];
echo '<option>'.$exchange.'</option>';
echo "\r\n";
}
?>
</select></p>

Job Number:
<p><select id="jobnoselect">
<option>Choose</option>
</select></p>

</div> <!-- /perex -->
</body>


Code from getter.php is

<?php

$username = "user";
$password = "passs";
$hostname = "host";

$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$selected = mysql_select_db("database", $dbhandle) or die("Could not select examples");
$choice = mysql_real_escape_string($_GET['choice']);

$query = "SELECT * FROM MasterJobTable WHERE Exchange='$choice'";

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
echo "<option>" . $row['JobNo'] . "</option>";
}
?>


Getter.php is returning the data and the jQuery is doing something as can be seen at the site http://btstats.000webhost.com/index2.html and getter can be directly accessed at http://btstats.host22.com/getter.php?=York

Does anyone have a possible explination why this doesn't work?

Answer

You aren't setting a value in

echo <option value="">'.$exchange.'</option>';

but you are querying it with ajax in

$("#jobnoselect").load("getter.php?choice=" + $("#exchangenameselect").val());

Try this:

echo <option value="'.$exchange.'">'.$exchange.'</option>';