简体   繁体   English

需要帮助,根据用户在第一个下拉框中选择的内容创建第二个选项下拉框

[英]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. 我已经能够从mysql数据库中获得要显示的汽车制造商选项,但是现在我只需要该制造商的汽车模型。 My data base has two collumns, one for the Make and one for the Model. 我的数据库有两个列,一个用于Make,一个用于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. PHP的顶部是我从单独的数据库中获取品牌的方法,而底部是我试图从具有所有汽车的make和model的数据库中获取模型的方法,但它显示了数百种模型,而不仅仅是少数几种我想要。 I heard that AJAX or javascript can automatically upload the results, which would be nice. 我听说AJAX或javascript可以自动上传结果,这很好。 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. 您是否听说过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. 在其他方面,JQuery使简化AJAX调用变得相当容易-我曾经在Web开发上投入的最佳时间是学习JQuery。 See http://api.jquery.com/jQuery.ajax/ for the JQuery docs for the AJAX method. 有关AJAX方法的JQuery文档,请参见http://api.jquery.com/jQuery.ajax/ 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 还有一些插件可以使用简单的代码来处理级联下拉列表,例如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. 一旦开始使用JQuery,您将发现最困难的事情是获得良好的汽车制造商和型号清单-暗示它们也因型号年份而异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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