在下拉选择中从数据库获取表值

时间:2016-12-19 15:24:32

标签: javascript php sql-server ajax

我有一个下拉列表和2个查询。每当我在下拉列表中选择一个选项时,我希望与数据库中的选择相关的行显示在表中。如果我回显应该获取并显示值的查询,并将其粘贴到我的SQLPro Studio中,它就可以工作了!但是,它在运行脚本时不起作用!为什么会出现这个问题?

填充下拉列表的查询:

myLocale

查询应引入与下拉选择相关的表值:

$sql = "
SELECT DISTINCT
       CONCAT(CAST(t1.MR_ID AS INT),' - ', COALESCE(t2.MR_Name, '')) AS MR_ID,
       t1.MR_ID AS sort_column 
FROM Stage_Rebate_Index t1
      LEFT JOIN Stage_Rebate_Master t2
         ON t2.MR_ID = t1.MR_ID
ORDER BY sort_column";

获取所选内容的Ajax:

$sql_one = "
SELECT 
       CONCAT(CAST(t1.MR_ID AS INT),' - ', COALESCE(t2.MR_Name, '')) AS MR_ID,
       t1.MR_ID AS sort_column, 
       CAST(Supp_ID as INT) AS Supp_ID
FROM Stage_Rebate_Index t1
      LEFT JOIN Stage_Rebate_Master t2
         ON t2.MR_ID = t1.MR_ID
WHERE  
  CONCAT(CAST(t1.MR_ID AS INT),' - ', t2.MR_Name) = LTRIM(RTRIM('$mr_id'))
ORDER BY sort_column";

从Ajax函数调用的PHP脚本:

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.
        console.log(data);
        console.log(selName)
    }
 });

}

0 个答案:

没有答案