[英]Dropdown that based on another dropdown, select option are from database
Sorry if it was discussed before, but all of them doesn't really work for me. 抱歉,如果以前没有讨论过,但是对我来说,所有这些都不起作用。 I have 5 dropdowns they are Brand, Model, Color, Engine No. and Chassis No., My question is what should I do to make the dropdown of the model is based on the selected dropdown of Brand, for example If a user selects Honda well based on my posted image in my database, the BrandID of Honda is 1 so all of the model that has the BrandID = 1. The dropdown of the model shown are only that has a brandID =1.
我有5个下拉菜单,分别是“品牌”,“型号”,“颜色”,“引擎号”和“底盘编号”。我的问题是我应该怎么做才能使模型的下拉列表基于所选的“品牌”下拉列表,例如,如果用户选择了“本田”根据我在数据库中发布的图像,本田的BrandID为1,因此所有具有BrandID = 1的模型都将显示。该模型的下拉列表仅具有brandID = 1。 Then the dropdown of color is based on the dropdown of the model, so the same logic like I discussed earlier.
然后,颜色的下拉列表基于模型的下拉列表,因此与我之前讨论的逻辑相同。 Finally the Dropdown of Engine No. and Chasis No. is based on the dropdown of the color, also the same as the logic that I discussed.
最后,引擎编号和底盘编号的下拉列表基于颜色的下拉列表,也与我所讨论的逻辑相同。
Here's my code for the brand dropdown 这是我的品牌下拉列表代码
<label for="bName" class="control-label col-xs-4"><p class="left">Brand</p></label> <div class="col-xs-7"> <div class="req"> <?php include_once "config.php"; $bsql="SELECT bName, brandID FROM brand order by bName"; $bstmt=$con->prepare($bsql); $bstmt->execute(); $bstmt->bind_result($bName, $bid); $bstmt->store_result(); echo "<select name='brandID' class='form-control'> <option value=''></option>"; while ($bstmt->fetch()){ echo '<option value="'.$bid.'">'.$bName.'</option>'; } echo '</select>'; ?> </div> </div>
Heres the code for the dropdown of the model 这是模型下拉菜单的代码
<label for="model" class="control-label col-xs-4"> <p class="left">Model</p></label> <div class="col-xs-7"> <div class="req"> <?php include_once "config.php"; $msql="SELECT model, modID FROM model order by model"; $mstmt=$con->prepare($msql); //$mstmt->bind_param('i', $bid); $mstmt->execute(); $mstmt->bind_result($model, $mid); $mstmt->store_result(); echo "<select name='mID' class='form-control'> <option value=''></option>"; while ($mstmt->fetch()){ echo '<option value="'.$mid.'">'.$model.'</option>'; } echo '</select>'; ?> </div> </div>
Heres the code for color dropdown 这是颜色下拉列表的代码
<label for="model" class="control-label col-xs-4"><p class="left">Color</p></label> <div class="col-xs-7"> <div class="req"> <?php include_once "config.php"; $csql="SELECT distinct(color) FROM stock order by color"; $cstmt=$con->prepare($csql); $cstmt->execute(); $cstmt->bind_result($color); $cstmt->store_result(); echo "<select name='color' class='form-control'> <option value=''></option>"; while ($cstmt->fetch()){ echo '<option value="'.$color.'">'.$color.'</option>'; } echo '</select>'; ?> </div> </div>
Heres the code for the dropdown Engine No. 这是下拉引擎编号的代码。
<label for="engNum" class="control-label col-xs-4"> <p class="left">Engine No</p></label> <div class="col-xs-7"> <div class="req"> <?php include_once "config.php"; $esql="SELECT engNum FROM stock where status='Available' order by engNum"; $estmt=$con->prepare($esql); $estmt->execute(); $estmt->bind_result($engNum); $estmt->store_result(); echo "<select name='engNum' class='form-control'> <option value=''></option>"; while ($estmt->fetch()){ echo '<option value="'.$engNum.'">'.$engNum.'</option>'; } echo '</select>'; ?> </div> </div>
Heres the code for the dropdown Chasis No. 这是下拉框编号的代码。
<label for="chassisNum" class="control-label col-xs-4"><p class="left">Chassis No</p></label> <div class="col-xs-7"> <div class="req"> <?php include_once "config.php"; $nsql="SELECT chassisNum FROM stock where status='Available' order by chassisNum"; $nstmt=$con->prepare($nsql); $nstmt->execute(); $nstmt->bind_result($chassisNum); $nstmt->store_result(); echo "<select name='chassisNum' class='form-control'> <option value=''></option>"; while ($nstmt->fetch()){ echo '<option value="'.$chassisNum.'">'.$chassisNum.'</option>'; } echo '</select>'; ?> </div> </div>
Heres the Image for my brand database 这是我品牌数据库的图片
Heres the Image for my model database 这是我的模型数据库的图像
Heres the Image for the color, chasis no. 这是颜色的图像,底盘编号。 and engine no.
和引擎号 database
数据库
Write an onchange event on select tag
for e.g. to change the Models based on brand selected you should write
<label for="bName" class="control-label col-xs-4"><p class="left">Brand</p></label>
<div class="col-xs-7">
<div class="req">
<?php
include_once "config.php";
$bsql="SELECT bName, brandID FROM brand order by bName";
$bstmt=$con->prepare($bsql);
$bstmt->execute();
$bstmt->bind_result($bName, $bid);
$bstmt->store_result();
echo "<select name='brandID' class='form-control' **onchange='getModels(this)'**>
<option value=''></option>";
while ($bstmt->fetch()){
echo '<option value="'.$bid.'">'.$bName.'</option>';
}
echo '</select>';
//The function getModels(this) will get called whenever user will change the value of the brand option.
Now define this method in your js file
function getModels(BrandObj)
{
brandValue=BrandObj.value; // Will give you the ID of brand which is selected.
// Make A ajax call to some php file which will return models based on brand ID & bind it to your Models Dropdown
$.ajax({
url: 'getModels.php',
type: 'GET', // Method Type
data: 'brandID=brandValue',// This parameter will be sent to getModels.php
success: function(**data**) {
//called when successful the data we have returned in getModels.php will be accessible in "data" variable
// Decode the response & bind it to your dropdownList
},
error: function(e) {
//called when there is an error
//console.log(e.message);
}
});
}
// IN your getModels.php file write following code
$BrandID=@$_GET['brandID'];
//Connect to database
// Write a sql to find models having brand_id=$BrandID
// Fetch rows & create array of Models & return it using
echo json_encode(*your_array_name*)
// END getModels.php
// You can find the detailed documentation of AJAX
http://api.jquery.com/jquery.ajax/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.