根据下拉选择显示表值

时间:2016-12-14 20:16:46

标签: javascript php ajax common-table-expression

我有一个下拉列表,该列表使用数据库中的表中的数据导入。下拉值由值MR_ID组成,该值与位于不同表中的MR_Name连接。根据所选内容,我想显示一个表格,该表格显示与下拉列表中所选内容相关的必要信息。因此,例如,如果我选择" 1 - 公司A"在下拉列表中,我想要与" 1 - 公司A"相关的Supp_ID值(可以超过1个值)。要在表格中显示的值。我怎么能这样做?

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

之前我有这个工作,但我不得不改变我的查询而不是使用CTE而且它让我失望。

以下是我在主页面上的原始查询以及ajax调用的查询...

// Query on main page
$sql = "SELECT DISTINCT CAST(MR_ID AS INT) AS MR_ID FROM Stage_Rebate_Index";

// 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'";

以下是我使用CTE的新查询,我无法弄清楚如何让它们正常工作......

// Query on main page
$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;";

// 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以及一行带来$ _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方法引入与ajax调用中使用的查询相同的脚本...

$mr_id = $_POST['mr_id'];

0 个答案:

没有答案