简体   繁体   English

从数据库填充下拉列表框而无需重新加载页面

[英]Populating a dropdown box from a database without reloading the page

I have a database with, say, 450 items in it. 我有一个包含450个项目的数据库。 Each item has a model number, and a manufacturer ID. 每个项目都有一个型号和一个制造商ID。

On a search page I have the option to search by Make & Model, but the lists are kind of messy. 在搜索页面上,我可以选择按品牌和型号进行搜索,但是列表有些混乱。

Currently, you have to select a make, then hit submit, and then you have another dropdown with a list of models with the same make. 当前,您必须选择一个品牌,然后单击“提交”,然后您将在另一个下拉列表中找到具有相同品牌的模型。

I would like to make it so that when you select the make it will automatically populate the model dropdown selection, using javascript, ajax, jquery or really anything that would make this possible. 我想这样做,以便当您选择make时,它会使用javascript,ajax,jquery或其他任何能够自动实现的模型下拉列表自动填充。

I don't have much to go on here as I don't really know the languages well. 我在这里没有太多工作,因为我不太了解这些语言。 The real problem I am having is that I don't know how to dynamically fill an array from a database based on what is selected from the first dropdown and then use it to populate the second dropdown. 我遇到的真正问题是,我不知道如何基于从第一个下拉列表中选择的内容来动态填充数据库中的数组,然后使用它来填充第二个下拉列表。

Any advice/working solutions would be greatly appreciated. 任何建议/工作解决方案将不胜感激。

NOTES: Currently, as I said before, you have to select the make, hit submit, then select a model - this is my current code for this, although I don't think it will be much use, using $_GET. 注意:当前,正如我之前说过的,您必须选择制造商,然后单击“提交”,然后选择一个模型-这是我目前的代码,尽管我认为使用$ _GET并不会用很多。

<div class="panel panel-info">
                  <div class="panel-heading">
                    <h3 class="panel-title">Model Search</h3>
                  </div>
                  <div class="panel-body">
                    <ul class="nav nav-tabs" role="tablist">
                        <li role="presentation" class="active"><a href="#type" aria-controls="type" role="tab" data-toggle="tab">Make</a></li>
                        <li role="presentation"><a href="#model" aria-controls="model" role="tab" data-toggle="tab">Model</a></li>
                    </ul>
                    <div class="tab-content">
                      <div role="tabpanel" class="tab-pane fade in active" id="type">
                          <br>
                                <form method="GET">
                                <div class="input-group">
                                    <?php
                                    $check = mysqli_query($con, "SELECT DISTINCT Make FROM Products ORDER BY Make ASC");
                                        echo '<select name="MAKE" style="width:100%;height:33px;">';
                                            if(strcmp($_GET['MAKE'], '') != 0)
                                                echo '<option value="' . $_GET['MAKE'] . '">' . $_GET['MAKE'] . '</option>';
                                            while($row = mysqli_fetch_array($check)) {
                                                echo '<option value="' . $row["Make"] . '">' . $row["Make"] . '</option>';
                                            }
                                        echo '</select>';
                                    ?>
                                  <span class="input-group-btn">
                                    <button class="btn btn-default" onclick="window.location.href='#model'" type="submit">Find Models</button>
                                  </span>
                                </div><!-- /input-group -->
                                </form>
                      </div>
                      <div role="tabpanel" class="tab-pane fade" id="model">
                                <br>
                                <form method="GET">
                                <div class="input-group">
                                    <?php
                                    $Make = $_GET['MAKE'];
                                    $check = mysqli_query($con, "SELECT * FROM Products WHERE `Make`='$Make' ORDER BY Model ASC");
                                        echo '<input type="hidden" name="MAKE" value="' . $Make . '">';
                                        echo '<select name="UPC" style="width:100%;height:33px;">';
                                            while($row = mysqli_fetch_array($check)) {
                                                echo '<option value="' . $row["UPC"] . '">' . $row["Model"] . '</option>';
                                            }
                                        echo '</select>';
                                    ?>
                                  <span class="input-group-btn">
                                    <button class="btn btn-default" onclick="window.location.href='#model'" type="submit">Search!</button>
                                  </span>
                                </div><!-- /input-group -->
                                </form>
                      </div>
                    </div>
                  </div>
                  <div class="panel-footer"></div>
            </div>

Probably the best way to do this would be to put the PHP code generating the options list in its own separate file, say get_options.php . 可能最好的方法是将生成选项列表的PHP代码放在自己的单独文件中,例如get_options.php Have that return a list of options, in the format 让该返回格式的选项列表

<option value="someValue">someValueName</option>
<option value="someOtherValue">someOtherValueName</option>
<option value="anotherValue">anotherValueName</option>

using the script that you were using before. 使用之前使用的脚本。

Then, in the initial page, have it make an AJAX request to get_options.php , for example like this: 然后,在初始页面中,让它向get_options.php发出AJAX请求,例如:

function getOptions() {
    make=document.getElementById("make"); //replace with where you actually get the make from
    var xmlRequest = new XMLHttpRequest();
    xmlRequest.onreadystatechange=function(){
        if (xmlRequest.readyState==4 && xmlRequest.status==200) {
            document.getElementById("someSelector").innerHTML = xmlRequest.responseText;
            //someSelector is the ID of your <select> tag.
        }
    }
    xmlRequest.open("GET","get_options.php?MAKE="+make,true);
    xmlRequest.send();
}

This script will get the make of the car from an input (I'm assuming probably a text input), send it off to get_options.php , recieve the list of <option> s and fill the selector ( id="someSelector" ) with said options. 该脚本将从输入中获取汽车的名称(我假设可能是文本输入),将其发送至get_options.php ,接收<option>的列表并填充选择器( id="someSelector" )有上述选择。 Since is is an AJAX request, rather that PHP, it can be executed at any point after the document has loaded, and will perform the same function. 由于是AJAX请求而不是PHP,因此可以在文档加载后的任何时间执行它,并执行相同的功能。 I would recommend using it in the onchange event of the make selector, for example 我建议在make选择器的onchange事件中使用它,例如

<input type="text" id="make" onchange="getOptions();">

You can use a simple jquery-ajax: 您可以使用简单的jquery-ajax:

first provide a id to select statement for first selector as 首先为第一个选择器提供一个id以选择语句

<select name="MAKE" style="width:100%;height:33px;" id="make">

now in jquery 现在在jQuery

 <script>
    $(function(){
$('#make').change(function(){
var opt=    $('#make').val();
$.ajax({
        url: 'your url of page where you get all data to show according to the value received from your first selector ',
        type: 'get',
        data: opt(data you want to send to get option),
        success: function(response) {
            //response contains all the data you wish to populate and using jquery populate to the select option
        }
    });

});



})
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM