POST变量在查询中无法正常工作

时间:2016-12-16 18:41:34

标签: php sql-server ajax post

我有一个下拉列表,选中后,我希望它显示与选择对应的值。为了测试它,我回应了我的查询,使用在我的SQLPro Studio中回显的代码,它工作得很好。但是,出于某种原因,它在我的脚本中不起作用。该值通过POST通过AJAX拉入。我在大多数事情上都做了console.log(),并且无法找到我的代码有什么问题。有人可以就如何解决这个问题提出一些建议吗?

test-table.php脚本:

<?php
$host="xxxxxxxxxxxxxx"; 
$dbName="xxxxx"; 
$dbUser="xxxxxxxxxx"; 
$dbPass="xxxxxxxx";

$mr_id = $_POST['mr_id'];

$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 Stage_Rebate_Index WHERE MR_ID = '$mr_id'";
//$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'";

$sql_one = "
SELECT 
       CONCAT(CAST(t1.MR_ID AS INT),' - ', 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";


//$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>

AJAX:

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

}

0 个答案:

没有答案