我有一個下拉列表,它是從數據庫中的一個表格導入的。下拉值由與位於不同表中的MR_Name連接的值MR_ID組成。根據所選內容,我想顯示一個表格,其中顯示了與下拉列表中所選內容相關的必要信息。因此,例如,如果我在下拉列表中選擇「1 - 公司A」,我想要在表格中顯示與「1 - 公司A」值相關的Supp_ID值(可以多於1個值)。我怎樣才能做到這一點?基於下拉選擇的顯示錶格
該表只有2列,MR_ID(這是下拉列表中顯示的內容)和Supp_ID。
我有這個工作之前,但我不得不改變我的查詢使用CTE來代替,它把我扔了。
HTML下拉:
<!-- Form -->
<form name="myForm" action="">
<!-- Dropdown List -->
<select name="master_dropdown" id="mr_id">
<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>
這裏是我的新老查詢我的主網頁(的index.php)上...
// Old query on main page (index.php)
$sql = "SELECT DISTINCT CAST(MR_ID AS INT) AS MR_ID FROM Stage_Rebate_Index";
// New query on main page (index.php)
$sql = "WITH cte AS (
SELECT DISTINCT CONCAT(CAST(Stage_Rebate_Index.MR_ID AS INT),' - ', Stage_Rebate_Master.MR_Name) AS MR_ID
, Stage_Rebate_Index.MR_ID AS sort_column
FROM Stage_Rebate_Index
LEFT JOIN Stage_Rebate_Master
ON Stage_Rebate_Master.MR_ID=Stage_Rebate_Index.MR_ID
)
SELECT MR_ID
FROM
cte
ORDER BY
sort_column;";
這裏是我的新老查詢我的測試-table.php頁
// Query that is used in ajax call (test-table.php)
$sql_one = "SELECT CAST(Supp_ID AS INT) AS Supp_ID, CAST(MR_ID AS INT) AS MR_ID FROM Stage_Rebate_Index WHERE MR_ID = '$mr_id'";
// Query that is used in ajax call (test-table.php)
$sql_one = "WITH cte AS (
SELECT DISTINCT CONCAT(CAST(Stage_Rebate_Index.MR_ID AS INT),' - ', Stage_Rebate_Master.MR_Name) AS MR_ID
, Stage_Rebate_Index.MR_ID AS sort_column, CAST(Stage_Rebate_Index.Supp_ID as INT) AS Supp_ID
FROM Stage_Rebate_Index
LEFT JOIN Stage_Rebate_Master
ON Stage_Rebate_Master.MR_ID=Stage_Rebate_Index.MR_ID
)
SELECT MR_ID, Supp_ID
FROM
cte
WHERE
MR_ID = '$mr_id'
ORDER BY
sort_column;";
這是我的AJAX(index.js)用一行代碼,在$ _POST值帶來沿...
// Reads what the user selects from the drop down list and displays table when a selection is made
function updatetable(myForm) {
function show() { document.getElementById('index-table').style.display = 'block'; }
var selIndex = myForm.selectedIndex;
console.log();
var selName = $("#mr_id option:selected").text();
// Ajax sends POST method to Stage_Rebate_Index table and pulls information based on drop down selection
$.ajax ({
url: "test-table.php",
method: "POST", //can be post or get, up to you
data: {
mr_id : selName
},
beforeSend: function() {
//Might want to delete table and put a loading screen, otherwise ignore this
},
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.
}
});
}
$ _ POST方法帶來的測試table.php ...
$mr_id = $_POST['mr_id'];
測試table.php腳本...
<?php
$host="xxxxxxxxxxxx";
$dbName="xxxxx";
$dbUser="xxxxxxxx";
$dbPass="xxxxxxxxxxxxxx";
$mr_id = $_POST['mr_id'];
$dbh = new PDO("sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql_one = "WITH cte AS (
SELECT DISTINCT CONCAT(CAST(Stage_Rebate_Index.MR_ID AS INT),' - ', Stage_Rebate_Master.MR_Name) AS MR_ID
, Stage_Rebate_Index.MR_ID AS sort_column, CAST(Supp_ID as INT) AS Supp_ID
FROM Stage_Rebate_Index
LEFT JOIN Stage_Rebate_Master
ON Stage_Rebate_Master.MR_ID=Stage_Rebate_Index.MR_ID
)
SELECT MR_ID, Supp_ID
FROM
cte
WHERE
MR_ID = '$mr_id'
ORDER BY
sort_column;";
//$users = $dbh->query($sql);
$users_one = $dbh->query($sql_one);
?>
<html>
<body>
<!-- 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>
</tr>
</thead>
<?php foreach($users_one->fetchAll() as $supp) { ?>
<tr>
<td class="mr_id"><?php echo $supp['MR_ID'];?></td>
<td class="supp_id"><?php echo $supp['Supp_ID'];?></td>
</tr>
<?php } ?>
</table>
</div>
</body
</html
MR_Name是一個'NVARCHAR' ...如果我在我正在使用的SQLPro Studio中運行這兩個查詢,兩次都會返回我需要的結果 – Rataiczak24
並且當您在您的Ajax中console.log數據變量'onSuccess'函數 - 是否記錄您期待的結果? – Sabir
在控制檯中,它會根據我的下拉列表選項顯示值的表格的HTML代碼 – Rataiczak24