javascript - 基于另一个下拉列表的下拉列表,选择选项来自数据库

标签 javascript php jquery html drop-down-menu

抱歉,如果之前讨论过,但所有这些都对我来说并不真正有用。我有 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>

这是我的品牌数据库的图片

enter image description here

这是我的模型数据库的图像

enter image description here

这是颜色的图像,底盘编号。和发动机号。数据库

enter image description here

最佳答案

    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/

关于javascript - 基于另一个下拉列表的下拉列表,选择选项来自数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33094136/

相关文章:

javascript - 我在数组中预加载图像,无法调用 ".src"属性?

php - Laravel 5.1 中的多步注册

javascript - 模式打开后 Bootstrap 3 画廊自动滑动激活?

javascript - 如何动态地将javascript附加到href?

javascript - mouseenter 更改 anchor 内的 HTML 后 mouseleave 未触发

javascript - 无法获取 onclick 事件来触发我在 javascript 中的功能

javascript - 在 CoffeeScript 中声明 Backbone 集合

javascript - 如何使用 flex-box 容器在多行上保留间距模板

php - pdo 仅使用查询从符合模式的表中获取列名

php - 如何在 PHP 单元测试中处理对未定义函数的调用