简体   繁体   English

在不同的HTML下拉菜单中显示数据库值

[英]Display Database Values on Different HTML Dropdown Selections

I have a dropdown list that is imported with data from a table in a database. 我有一个下拉列表,该列表与数据库表中的数据一起导入。 Depending on what is selected, I want to display a table that shows the necessary information that correlates to the number that was selected in the dropdown list. 根据选择的内容,我想显示一个表,该表显示与下拉列表中选择的数字相关的必要信息。 So, for example, if I select a MR_ID of 1 in the dropdown list, I want the Supp_ID values (can be more than 1 value) to be displayed in a table. 因此,例如,如果我在下拉列表中选择MR_ID为1,则我希望Supp_ID值(可以大于1个值)显示在表中。 How can I do this? 我怎样才能做到这一点?

The table is only 2 columns, MR_ID (which is what is displayed in the dropdown list) and Supp_ID. 该表只有2列,即MR_ID(下拉列表中显示的内容)和Supp_ID。

Here are my queries used for the foreach loops, my HTML/PHP that I have so far, also followed by the little bit of JavaScript 这是我用于foreach循环的查询,到目前为止我的HTML / PHP,还有一些JavaScript

$dbh = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT DISTINCT CAST(MR_ID AS INT) AS MR_ID FROM Table_Name";
$sql_one = "SELECT CAST(Supp_ID AS INT) AS Supp_ID FROM Table_Name";

$users = $dbh->query($sql);
$users_one = $dbh->query($sql_one);

HTML/PHP: HTML / PHP:

    <!-- Dropdown List -->
    <select name="master_dropdown" id="mr_id" onChange="updatetable(this.form);">
    <option selected value="select">Choose a MR_ID</option>
        <?php foreach($users->fetchAll() as $user) { ?>
            <option data-name="<?php echo $user['MR_ID'];?>">
                <?php echo $user['MR_ID'];?>
            </option>
        <?php } ?>
    </select>
</form>

<!-- Table -->
<p> 
    <div id="table_div">
        <table border="1" id="index_table" class="ui-widget ui-widget-content">
            <thead>
                <tr class="ui-widget-header">
                <td>MR ID</td>
                <td>Supplier ID</td>
                <td>Edit</td>
                </tr>
            </thead>
            <?php foreach($users_one->fetchAll() as $supp) { ?>
            <tr>
                <td class="mr_id"><div id="dropdown_selection"></div></td>
                <td class="supp_id"><?php echo $supp['Supp_ID']?></td>
                <td><input type="button" class="edit" name="edit" value="Edit">
            </tr>
            <?php } ?>
        </table>
    </div>

This JavaScript code reads what was selected in the dropdown list but that is the extent of what it does. 此JavaScript代码读取下拉列表中选择的内容,但这就是它所做的工作的程度。

function updatetable(myForm) {

    var selIndex = myForm.master_dropdown.selectedIndex;
    var selName = myForm.master_dropdown.options[selIndex].text;
    document.getElementById("dropdown_selection").innerHTML = String(selName);
}

The ajax should look something like this: Ajax应该看起来像这样:

$.ajax ({
    url: "table_drawing.php",
    method: "get", //can be post or get, up to you
    data: {
        mr_id : mr_id    
    },
    beforeSend: function () {
        //Might want to delete table and put a loading screen, otherwise ignore this
        // $("#table_div").html(loading_screen);
    },
    success: function(data){
        $("#table_div").html(data); // table_div is the div you're going to put the table into, and 'data' is the table itself.
    }
});

In table_drawing.php you will be drawing the table based off of mr_id's input. 在table_drawing.php中,您将根据mr_id的输入绘制表格。

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

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