I have a dropdown list that when selected, I want it to display values that correspond to the selection. I echo out my query that I have in order to test it, use the code that is echoed in my SQLPro Studio, and it works great. However, for some reason, it is not working in my script. The value is pulled in through POST via AJAX. I have done a console.log()
on most things and cant find anything wrong with my code. Can someone please offer up some advice in how to fix this?
test-table.php script:
<?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)
}
});
}