简体   繁体   English

使用mysql数据库根据第一个下拉框自动填充第二个下拉框

[英]Using a mysql data base to populate a second drop down box automatically based off of 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, and the bottom is my attempt to get the model, but it displays hundreds of models, instead of just the few I want. PHP的顶部是我获得商标的方式,底部是我尝试获得模型的方法,但是它显示了数百个模型,而不仅仅是我想要的几个。 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>
$sql = "SELECT Model FROM myTable";

This clearly is giving you the results you are asking for. 显然,这可以为您提供所需的结果。 It will select all models. 它将选择所有型号。

Your sql should look like 你的sql应该看起来像

$sql = "SELECT Model FROM myTable WHERE Make='Make'";

The where limits you to retrieve only models associated with the specific Make you have selected. 在哪里限制您仅检索与您选择的特定品牌相关的模型。

To do it with AJAX, are you using jQuery? 要使用AJAX做到这一点,您正在使用jQuery吗? Straight JavaScript with an Ajax call? 带有Ajax调用的直接JavaScript? Please provide more information for answering that part of your question. 请提供更多信息以回答问题的这一部分。

This probably isn't the best way to do this, but when I have had to do things like this before, I usually use the php to populate some javascript variables in my header and then use javascript to create the selects. 这可能不是执行此操作的最佳方法,但是当我之前不得不做类似的事情时,我通常使用php在标头中填充一些JavaScript变量,然后使用javascript创建选择。 Also, I'm assuming that there is something in your database that maps certain models to certain makes of vehicles. 另外,我假设您的数据库中有一些东西可以将某些模型映射到某些车辆型号。

<script>
var makes = new Array();
var models = new Array();
<?php
$sql = "SELECT Make FROM CarMakes";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
?>
    makes[] = <?php echo json_encode($row['Make']); ?>;
<?php
}
$sql = "SELECT Make FROM CarMakes";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
?>
    models[] = <?php echo json_encode($row['Model']); ?>;
<?php
}
?>
</script>

From here, you can create the selects and populate the options of the first one with the elements of the makes array. 在这里,您可以创建选择并使用make数组的元素填充第一个选项。 Then add an onChange event to the first select to populate the second select. 然后将onChange事件添加到第一个选择中以填充第二个选择。

EDIT For select creation: 编辑对于选择创建:

<select id="make-select" onChange="updateModels()">
</select>
<select id="model-select">
</select>
<script>
$(function() {
    $.each(makes, function (index, value) {
        $(#make-select).append($('<option>') {
            value: value,
            text: value
        });
    });
}
</script>

Then just create a function that called updateModels that will update model-select based on the selected value of make-select . 然后只需创建一个名为updateModels的函数,该函数将根据make-select的选定值更新model-select make-select Again, you'll need somehting that associates models with makes in your database, and then you might have to make the makes variable an object instead of just an array. 同样,你需要服用点与数据库中的品牌关联模型,那么你可能必须做出makes变量的对象,而不是仅仅一个阵列。

var models = new Object()
<?php
    $sql = "SELECT Model-Make, Model FROM CarMakes";
    ...
    models.make = <?php echo json_encode($row['Model-Make']); ?>;
    models.model = <?php echo json_encode($row['Model']); ?>;
    ...
?>

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

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