简体   繁体   中英

Need help creating a second drop down box of options based on what a user selects in the first drop down box

I need to have a selection box display options that are based off of the selection from the drop down box right above it. Once the user selects the Car Make, then I want the Car Models of the car make to be options to be selected.

I have been able to get the car makes options to be displayed from my mysql data base, but now I need the car models of only that make. My data base has two collumns, one for the Make and one for the Model.

The top section of PHP is the way i get the make from a seperate database, and the bottom is my attempt to get the model from a database with makes and models of all cars, but it displays hundreds of models, instead of just the few I want. I heard that AJAX or javascript can automatically upload the results, which would be nice. Any help is great. thanks!

</div>

<?php
    mysql_connect('localhost', '*****', '******');
    mysql_select_db('*************');
    $sql = "SELECT Make FROM CarMakes";
    $result = mysql_query($sql);
    echo "<select name='carmake3'>";
    while ($row = mysql_fetch_array($result)) {
        echo "<option value='" . $row['Make'] . "'>" . $row['Make'] . "</option>";
    }
    echo "</select>";
?>
<?php
    mysql_connect('localhost', '******', '**********');
    mysql_select_db('*************');
    $sql = "SELECT Model FROM myTable";
    $result = mysql_query($sql);
    echo "<select class='modelbox' name='carmodel3'>";
    while ($row = mysql_fetch_array($result)) {
        echo "<option value='" . $row['Model'] . "'>" . $row['Model'] . "</option>";
    }
    echo "</select>";
?>
<input type="text" maxlength="4" name="car3year" placeholder="year" class="WriteInBox"/>

<input type="text" maxlength="6" name="car3miles" placeholder="miles" class="WriteInBox"/>

</div>

Have you ever heard of JQuery. If not, it time for some education. Among a host of other things, JQuery makes it fairly easy to simpllify your AJAX calls -- Best time I ever invested in web development was learning JQuery. See http://api.jquery.com/jQuery.ajax/ for the JQuery docs for the AJAX method. There are some simple usage examples at the bottom of the page. There are also plugins that handle cascading dropdowns lists with simple code, like this one http://jquery-plugins.net/jquery-cascading-dropdown-plugin

Once you start using JQuery, you will discover the hardest thing is getting good lists of car makes and models -- hint they vary by model year too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM