问题描述
我有这个选择,让我们称之为X,通过SELECT将数据库中的汽车品牌填充为选项。
I'm having this select, let's call it "X", that is populated with car brands as options from the database via a 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>
现在,我必须填写第二个选择,称为Y,其中包含品牌的型号选择
Now i have to populate the second select, called "Y", with the models specifics to a brand selected.
例如,如果在第一个选择框(X)中选择的选项是奥迪,我应该在第二个选择(Y)中具有以下选项: A4,A6,TT,TTs
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
手动填充第二个选择框很简单,基本上与第一个只是使用不同的SQL请求相同的东西。 >
To populate the second select box manually is easy, basicaly the same thing as for the first just with a different SQL request.
$row = mysqli_query("SELECT modelName from modele WHERE brandName = '$brand'");
根据第一选择的选择,$品牌将有一个值。
Where $brand would have a value according to the first select's selection.
谢谢
推荐答案
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.
这篇关于根据第一个选择框选项填充第二个选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!