How to Put Table Name In a VARIABLE ??

How to Put Table Name In a VARIABLE ??

daguerfidaguerfi Posts: 6Questions: 3Answers: 0
edited February 2017 in Free community support

Hi, I want to change the database table name from the client side via an input form. How To post the input value to the server-side?

Client side:

$(document).ready(function() {
var tid = $('#table_id').val();
    $('#tb-data').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "server_processing.php",
        "type": "POST",
                // What to put here?  :/  :o 

     } );
                        
} );

Server side:

// DB table to use $table = $_POST['table_id'];

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin

    ajax.data will let you send parameter to the server.

    Allan

  • daguerfidaguerfi Posts: 6Questions: 3Answers: 0
    edited February 2017

    Thanks Allan, But how to read the data in the server side, I tried both Post and Get parameters with no luck. Is this how to use the data option?

            "data": function ( d ) {
                   d.table_name = $('#table_id').val();
             
                }
    
    

    How to read the posted data on the server-side? many thanks.

    // DB table to use
    $table = $_GET['table_name']
    
  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin
    Answer ✓

    d.table_name = $('#table_id').val();

    If your #table_id was an input element that returns a value, then yes. My guess is that that is actually your table and thus it won't have a value.

    How to read the posted data on the server-side?

    You've specifically marked it as POST so you would use $_POST['myVariable'] in PHP.

    Allan

  • daguerfidaguerfi Posts: 6Questions: 3Answers: 0
    edited February 2017

    Thanks Allan, It worked like a charm :smiley: God Bless.

    Please one last question if you have a time, how to reload the table with a new ajax call with the same columns, without getting this error 'Cannot Reinitialise Datatable'. I tried the 'destroy' option but I can't figure it out how to refill the table again.
    many thanks.

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin

    If you are getting that error, then you aren't destroying the table. You might want to use $.fn.dataTable.isDataTable() to check if a table is actually a DataTable or not before destroying it.

    Allan

  • daguerfidaguerfi Posts: 6Questions: 3Answers: 0

    Ok, I have managed to get it work, Allan thank you very much. o:)

    Here's the entire ajax call code for those who's struggling.

    <script type="text/javascript" class="init">
        $(document).ready(function() {
    
            // check if button is clicked - remove this if not needed
            $('#load_db').click(function() {
    
                // check if table is a DataTable then destroy old one
                var table = $('#tb-data').DataTable();
                if ($.fn.dataTable.isDataTable('#tb-data')) {
                    table.destroy();
                }
    
                // call new datatable and post additional data
                $('#tb-data').DataTable({
                    "processing": true,
                    "serverSide": true,
                    "ajax": {
                        "url": 'server_processing.php',
                        "data": {
                            "table_id": $('#table_id').val()
                        }
                    }
                });
    
                return false;
            });
    
        });
    </script>
    

    I also was having an issue when sorting numbers, they weren't sorting correctly, then later I figure out that I was storing them as a string in database instead of integers (INT).

    Hope It's helps someone. :)

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin

    That's the business! There is actually one other option that is a little more efficient - using ajax.url.reload() on an already initialised table and using ajax.data as a function so it will get the value every time it is submitted:

    <script type="text/javascript" class="init">
        $(document).ready(function() {
     
            // check if button is clicked - remove this if not needed
            $('#load_db').click(function() {
     
                // check if table is a DataTable then destroy old one
                var table = $('#tb-data').DataTable();
                if ($.fn.dataTable.isDataTable('#tb-data')) {
                     $('#tb-data').DataTable().ajax.reload();
                }
                else {
                  // call new datatable and post additional data
                  $('#tb-data').DataTable({
                      "processing": true,
                      "serverSide": true,
                      "ajax": {
                          "url": 'server_processing.php',
                          "data": function ( d ) {
                              d.table_id: $('#table_id').val();
                          }
                      }
                  }
                });
     
                return false;
            });
     
        });
    </script>
    

    Allan

This discussion has been closed.