【问题标题】:Display Database Values on Different HTML Dropdown Selections在不同的 HTML 下拉选择中显示数据库值
【发布时间】:2016-12-09 13:27:45
【问题描述】:

我有一个下拉列表,它是从数据库中的表中导入的数据。根据选择的内容,我想显示一个表格,其中显示与下拉列表中选择的数字相关的必要信息。因此,例如,如果我在下拉列表中选择 1 的 MR_ID,我希望 Supp_ID 值(可以大于 1 值)显示在表中。我该怎么做?

该表只有 2 列,MR_ID(下拉列表中显示的内容)和 Supp_ID。

这是我用于 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:

    <!-- 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>

此 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);
}

【问题讨论】:

  • MR_ID 和 SUPP_ID 有什么关系?表中没有所选 MR_ID 的关系
  • MR_ID 字段与此处未使用的另一个表中的字段相关。就我而言,与 Supp_ID 没有任何关系。
  • 您是在寻找 Ajax 数据,还是只是将数据存储在一个 javascript 变量中,当您更改选项时,该变量基本上会填充表格?
  • 不完全确定...什么是最好的?为了解决这个问题,我愿意接受任何一种方法
  • 通常情况下,ajaxing 是最好的,因为您可以调用您只需要的数据。

标签: javascript php jquery html


【解决方案1】:

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.
    }
});

在 table_drawing.php 中,您将根据 mr_id 的输入绘制表格。

【讨论】:

  • 很抱歉再次打扰,但是如果您看到这个,您可以加入我们在上面 cmets 中的聊天吗?我有一个问题。
猜你喜欢
  • 2015-10-24
  • 2018-12-01
  • 2013-02-19
  • 2014-02-26
  • 2020-10-11
  • 2016-12-04
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多