简体   繁体   中英

Display Database Values on Different HTML Dropdown Selections

I have a dropdown list that is imported with data from a table in a database. Depending on what is selected, I want to display a table that shows the necessary information that correlates to the number that was selected in the dropdown list. So, for example, if I select a MR_ID of 1 in the dropdown list, I want the Supp_ID values (can be more than 1 value) to be displayed in a table. How can I do this?

The table is only 2 columns, MR_ID (which is what is displayed in the dropdown list) and Supp_ID.

Here are my queries used for the foreach loops, my HTML/PHP that I have so far, also followed by the little bit of JavaScript

$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 Table_Name";
$sql_one = "SELECT CAST(Supp_ID AS INT) AS Supp_ID FROM Table_Name";

$users = $dbh->query($sql);
$users_one = $dbh->query($sql_one);

HTML/PHP:

    <!-- Dropdown List -->
    <select name="master_dropdown" id="mr_id" onChange="updatetable(this.form);">
    <option selected value="select">Choose a MR_ID</option>
        <?php foreach($users->fetchAll() as $user) { ?>
            <option data-name="<?php echo $user['MR_ID'];?>">
                <?php echo $user['MR_ID'];?>
            </option>
        <?php } ?>
    </select>
</form>

<!-- 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>
                <td>Edit</td>
                </tr>
            </thead>
            <?php foreach($users_one->fetchAll() as $supp) { ?>
            <tr>
                <td class="mr_id"><div id="dropdown_selection"></div></td>
                <td class="supp_id"><?php echo $supp['Supp_ID']?></td>
                <td><input type="button" class="edit" name="edit" value="Edit">
            </tr>
            <?php } ?>
        </table>
    </div>

This JavaScript code reads what was selected in the dropdown list but that is the extent of what it does.

function updatetable(myForm) {

    var selIndex = myForm.master_dropdown.selectedIndex;
    var selName = myForm.master_dropdown.options[selIndex].text;
    document.getElementById("dropdown_selection").innerHTML = String(selName);
}

The ajax should look something like this:

$.ajax ({
    url: "table_drawing.php",
    method: "get", //can be post or get, up to you
    data: {
        mr_id : mr_id    
    },
    beforeSend: function () {
        //Might want to delete table and put a loading screen, otherwise ignore this
        // $("#table_div").html(loading_screen);
    },
    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.
    }
});

In table_drawing.php you will be drawing the table based off of mr_id's input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM