我有一个下拉列表,其中包含数据库表中的数据。根据所选内容,我想显示一个表格,其中显示与下拉列表中所选数字相关的必要信息。因此,例如,如果我在下拉列表中选择 MR_ID 为 1,我希望 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);
}
最佳答案
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 的输入绘制表格。
关于javascript - 在不同的 HTML 下拉选项上显示数据库值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41061572/