0

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)
    }
 });

}
Rataiczak24
  • 1,032
  • 18
  • 53
  • First of all, you would want to create the table on client side not server side. You just fetch the neccessary row values for your table with ajax. So with other words, create the table in javascript, not in php. Would make debugging easier as well. – Andrew Larsen Dec 16 '16 at 18:45
  • What does `var_dump($_POST)` show? Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a *specific problem or error* and *the shortest code necessary* to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – elixenide Dec 16 '16 at 18:47
  • Also, you are wide open to [**SQL injection**](https://www.owasp.org/index.php/SQL_Injection). You need to use prepared statements, rather than concatenating variables into your query. See [How can I prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1). – elixenide Dec 16 '16 at 18:47
  • This is the value of that...array(1) { ["mr_id"]=> string(43) " 1 - Company A " } – Rataiczak24 Dec 16 '16 at 18:49
  • Just to get things straight here. Your test-table.php file is the one you are loading to see your table. And this is also the page were you have included your ajax, and the ajax request loads the same page? I see you use $("#table_div").html(data); which points to the table in test-table.php this would result in a table with a html document as content since you load the entire test-table.php file with ajax and put it as html in that table. – Andrew Larsen Dec 16 '16 at 18:54
  • I have an `index.php` file that is my main page...`test-table.php` is just the script that the AJAX calls – Rataiczak24 Dec 16 '16 at 18:57
  • Okay, but then again test-table.php will return a full html document that you again is placing within another html document. – Andrew Larsen Dec 16 '16 at 19:06
  • I had it like this before using different queries and it was working fine....but i had to make some changes to my queries with the concatenation and it messed things up – Rataiczak24 Dec 16 '16 at 19:08

0 Answers0