0

I am trying to populate the second dropdown from my choice in the second, but whenever I make my change nothing updates.

<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.

...

<select id="first-choice">
<option selected value="base">Please Select a Make</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>

...

Any reason why?

3
  • Have you checked your resulting SQL queries? Commented Apr 5, 2012 at 9:59
  • 1
    Try putting your script within $(function() {}) handler Commented Apr 5, 2012 at 10:02
  • 1
    Typo: $second-choice should be #second-choice Proof read a bit more ;-) Commented Apr 5, 2012 at 10:02

1 Answer 1

4

You have a typo here $("$second-choice")

$("#second-choice").load("findModel.php?choice=" + $("#first-choice").val());

EDIT - have you tried

$(function(){
    var second_choice = $('#second-choice').val();
    $("#first-choice").change(function() {
        $("$second-choice").load("findModel.php?choice=" + $("#first-choice").val());
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

@Mombassa i edited my answer, but with firebug do you see the call?Maybe you didn'0t bind the change event because you didn't use the ready() handler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.