EDIT: My main code no longer works, should this function work?
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script>
var second_choice = $('#second-choice').val();
$("#first-choice").change(function() {
$("$second-choice").load("findModel.php?choice=" + $("#first-choice").val());
});
</script>
Here is the associated PHP File:
<?php
include 'dbc.php';
$choice = mysql_real_escape_string($_GET['choice']);
$query="SELECT * FROM `cars` WHERE `DVLAMake`='$choice'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<option>" . $row{'DVLAModel'} . "</option>";
}
?>
The database connection works.
#
On load a PHP file populates my first dropdown:
<form name="indexSearch" action="searchResults.php" method="POST">
<select id="first-choice">
<option selected value="base">Please Select</option>
<option value="VAUXHALL">VAUXHALL</option>
<?php
$sql="SELECT DISTINCT `DVLAMake` FROM `cars`";
$result = mysql_query($sql);
while ($data=mysql_fetch_assoc($result))
{
echo "<option value =\"{$data[DVLAMake]}\" >{$data[DVLAMake]}</option>\n";
}
?>
</select>
<select id="second-choice">
<option>Please choose from above</option>
</select>
<br />
<input type="submit" style="font-size:14px; padding:3;"value="Submit" size="20" />
</form>
That works, and then on choosing the value it calls a function which then fills the second with options which I can choose from. However when I post the form, it doesn’t take the second dropdown selected value through it’s just empty but it does take the first dropdown value selected value.
Any reason why?
The problem is that the second select tag does not have name attribute. if it lies in the form you will get the request via post only if the attribute has name. if you are using jquery you can simply fetch value by id then post it via ajax. select like this in jquery.