![](/img/trans.png)
[英]Populate another select dropdown from database based on dropdown selection
[英]Dropdown that based on another dropdown, select option are from database
抱歉,如果以前沒有討論過,但是對我來說,所有這些都不起作用。 我有5個下拉菜單,分別是“品牌”,“型號”,“顏色”,“引擎號”和“底盤編號”。我的問題是我應該怎么做才能使模型的下拉列表基於所選的“品牌”下拉列表,例如,如果用戶選擇了“本田”根據我在數據庫中發布的圖像,本田的BrandID為1,因此所有具有BrandID = 1的模型都將顯示。該模型的下拉列表僅具有brandID = 1。 然后,顏色的下拉列表基於模型的下拉列表,因此與我之前討論的邏輯相同。 最后,引擎編號和底盤編號的下拉列表基於顏色的下拉列表,也與我所討論的邏輯相同。
這是我的品牌下拉列表代碼
<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>
這是模型下拉菜單的代碼
<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>
這是顏色下拉列表的代碼
<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>
這是下拉引擎編號的代碼。
<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>
這是下拉框編號的代碼。
<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>
這是我品牌數據庫的圖片
這是我的模型數據庫的圖像
這是顏色的圖像,底盤編號。 和引擎號 數據庫
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.