简体   繁体   English

根据第一个选择框选项填充第二个选择框

[英]Populating a second select box depending of the first select box option

I'm having this select, let's call it "X", that is populated with car brands as options from the database via a SELECT. 我有这个选择,让我们称之为“X”,通过SELECT从数据库填充汽车品牌作为选项。

<select name="X">
<?php
$row = mysqli_query("SELECT * FROM brands");
while($row2 = mysqli_fetch_array($row))
echo '<option value="' . $row2['brandID'] . '">' . $row2['brandName'] . '</option>
?>
</select>

Now i have to populate the second select, called "Y", with the models specifics to a brand selected. 现在我必须填充第二个选择,称为“Y”,其中包含所选品牌的特定模型。

For example, if the option that's selected in the first select box (X) is Audi i should have as options in the second select (Y) the following: A4,A6,TT,TTs 例如,如果在第一个选择框(X)中选择的选项是奥迪,我应该在第二个选择(Y)中选择以下选项:A4,A6,TT,TTs

To populate the second select box manually is easy, basicaly the same thing as for the first just with a different SQL request. 手动填充第二个选择框很容易,基本上与第一个只有不同的SQL请求相同。

$row = mysqli_query("SELECT modelName from modele WHERE brandName = '$brand'");

Where $brand would have a value according to the first select's selection. 根据第一个选择的选择,$ brand将有一个值。

Thanks 谢谢

Put an id in your <select name="X"> code like <select name="X" id ="X">

Put another select like <select name="Y" id="Y">. Which will be blank.

put this jquery in your page.

$("X").on("change",function(){
    var x_value=$("X").val();
    $.ajax({
        url:'ajax.php',
        data:{brand:x_value},
        type: 'post',
        success : function(resp){
            $("#Y").html(resp);               
        },
        error : function(resp){}
    });
});

in your ajax.php add the query.

<?php
$row = mysqli_query("SELECT modelName from modele WHERE brandName =".$_POST['brand']);
while($row2 = mysqli_fetch_array($row))
    echo '<option value="' . $row2['modelId'] . '">' . $row2['modelName'] . '</option>
?>

Hopefully it will work. 希望它能奏效。 Please tell me if you need anything. 请告诉我您是否需要任何东西。

Use ajax and on change event together, 一起使用ajax和change事件,

method: 1- build a page where you post your queries to. 方法:1-建立一个页面,您可以在其中发布查询。 2- that page should react to the query and sends a respond with the desired list of options. 2-该页面应对查询做出反应并发送带有所需选项列表的响应。 example : 例如:

if ($_POST["vehicle"] == car) // send response with array of car names as json obj for example
else // send response with array of motorbikes names

your ajax should receive it and populate the filed on-complete. 你的ajax应该收到它并填写完整的文件。

If you need more details, I'll be happy to provide it 如果您需要更多详细信息,我们将很乐意提供

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

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