基于另一个下拉菜单的下拉菜单,select选项来自数据库 [英] Dropdown that based on another dropdown, select option are from database

查看:170
本文介绍了基于另一个下拉菜单的下拉菜单,select选项来自数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果以前讨论过,对不起,但是他们都不为我工作。我有5个下拉菜单,他们是品牌,型号,颜色,发动机编号和底盘编号,我的问题是我该怎么做,使模型的下拉菜单基于所选的品牌下拉菜单,例如,如果用户选择本田基于我在我的数据库中发布的图像,本田的BrandID是1,所以所有的BrandID = 1的模型。所示模型的下拉列表只有brandID = 1。那么颜色的下拉是基于模型的下拉菜单,所以就像我之前讨论过的逻辑一样。最后,引擎号码和Chasis号码的下拉是基于颜色的下拉,也与我讨论的逻辑相同。



这是我的



 < label for = bNameclass =control-label col-xs-4>< p class =left>品牌< / p>< / label> < div class =col-xs-7> < div class =req> <?php include_onceconfig.php; $ bsql =SELECT bName,brandID FROM brand order by bName; $ bstmt = $ CON组>制备($ BSQL); $ bstmt->执行(); $ 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_onceconfig.php; $ msql =SELECT model,modID FROM model order by model; $ mstmt = $ CON组>制备($ mSQL的); // $ mstmt-> bind_param('i',$ bid); $ mstmt->执行(); $ 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 =modelclass = control-label col-xs-4>< p class =left> Color< / p>< / label> < div class =col-xs-7> < div class =req> <?php include_onceconfig.php; $ csql =SELECT distinct(color)FROM stock order by color; $ cstmt所= $ CON组>制备($ csql); $ cstmt->执行(); $ cstmt-> bind_result($颜色); $ 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_onceconfig.php; $ esql =SELECT engNum FROM stock where status ='Available'order by engNum; $ estmt = $ CON组>制备($ ESQL); $ estmt->执行(); $ 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>  



下拉菜单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> <?phpinclude_onceconfig.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。 '< /选项>'; } echo'< / select>'; ?> < / DIV> < / div>  



品牌数据库





将图像归为颜色,chasis no。和引擎号。数据库



解决方案

 在选择标签上编写一个onchange事件

,例如根据品牌选择更改模型您应该写


< label for =bNameclass =control-label col-xs-4>< p class = 左 >品牌< / p>< /标签>
< div class =col-xs-7>
< div class =req>
<?php
include_onceconfig.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 ='' >< /选项>中;

while($ bstmt-> fetch()){
echo'< option value ='$ bid。'>'。$ bName。'< /选项>';
}

echo'< / select>';

//当用户将更改品牌选项的值时,getModels(this)函数将被调用。

现在在你的js文件中定义这个方法


function getModels(BrandObj)
{
brandValue = BrandObj.value; //会给你所选的品牌ID。
// Make a ajax call to some php file which will return models based on brand ID&绑定到你的模型下拉
$ .ajax({
url:'getModels.php',
type:'GET',//方法类型
data:'brandID = brandValue',//这个参数将被发送给getModels.php
success:function(** data **){
//在成功时调用我们在getModels.php中返回的数据将被访问在数据变量
//解码响应&绑定到您的dropdownList

},
错误:function(e){
//调用时有一个错误
//console.log(e.message);
}
});


}


//在您的getModels.php文件中写入以下代码

$ BrandID = @ $ _ GET [ 'brandID'];
//连接到数据库
//编写一个sql来查找具有brand_id = $ BrandID
的模型//获取行&创建模型&返回它使用
echo json_encode(* your_array_name *)

// END getModels.php

//您可以找到AJAX
的详细文档http://api.jquery.com/jquery.ajax/


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. 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/

这篇关于基于另一个下拉菜单的下拉菜单,select选项来自数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆