I am trying to populate a second dropdown list using the value selected from a first dropdown list using PHP and MySQL, and without refreshing the page. I thought this would be simple but can't get it to work so any help would be much appreciated.
So far, I have the following:
HTML Form (form.php)
<select name="list1" id="list1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="list2" id="list2">
</select>
JavaScript (within form.php)
<script type="text/javascript">
$("#list1").change(function() {
$("#list2").load("get_list2.php?id=" + $("#list1").val());
});
</script>
get_list2.php
require_once("config.php");
$q1 = mysql_query("SELECT * FROM mytable WHERE id = '$_GET[id]'");
while($row1 = mysql_fetch_assoc($q1)){
echo "<option>".$row1['item']."</option>";
}
Thanks!