Home » Php » Show database tables selected by dropdown menu with datatables

Show database tables selected by dropdown menu with datatables

Posted by: admin October 26, 2017 Leave a comment

Questions:

i am very new to dataTables-PlugIn. I am trying to select only few tables on my Database via dropdown menu.

this is my html code:

<div class="select_country" align="center">
            <form method="post" action="table-data.php">
                <select id="select_table" name="select_table" class="select_table form-control">
                    <option value="land">LAND</option>  
                    <option value="id_hu">HU</option>   
                    <option value="id_no">NO</option>   
                    <option value="id_MX">MX</option>   
                    <option value="id_fr">FR</option>       
                </select>
                <input type="submit" value="search">
            </form> 
        </div>   

the dataTables code:

<script type="text/javascript" language="javascript" >
            $(document).ready(function() {              
                var dataTable = $('#mytable').DataTable( {       
                    "processing": true,
                    "serverSide": true, 
                    "searching": false,         
                    "ajax":{
                        url : "table-data.php", 
                        type: "post", 
                    },              
                } );
                    $('#select_table').change(function() {
                     var myValue= this.value;
                     var string = ('table-data.php?select_table='+myValue);
                     table.ajax.url(string).load();
                     table.reload();
                  });
            } );                        
        </script>  

and this is my php code:

include("_dbconn.php"); 
$selCountry = $_POST["select_table"];
if (isset($_POST["submit"])) {

    $selCountry = $_POST["select_table"];
    $tables = $conn->query("SELECT * FROM information_schema.tables WHERE table_schema = 'mydatabase' AND TABLE_NAME=$selCountry");

    while ($row = mysqli_fetch_row($tables)) {

        $table_name = $row[2];
        fwrite($flog, "\nselected table:" . $table_name);

    }  

my question is: how can i link the selected country from the dropdown menu to the php code to show the selected table into the dataTable.
Thank you!

Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *