-1

I'm using this example's code https://datatables.net/examples/server_side/simple.html

I want to use $_GET variable in the server_processing.php file so that I can specify the value and doing this from the url. For example table.php?table-id=table1

index.html

    <a href="table.php?table-id=table1">Table 1</a>
    <a href="table.php?table-id=table2">Table 2</a>
    <a href="table.php?table-id=table3">Table 3</a>

table.php

    <?php
    // Get
    $var = $_GET['table-id'];
    ?>
    <table id="example" style="width:100%">
        <thead>
            <tr>
                <th>Payslip ID</th>
                <th>Employee ID</th>
                <th>Employee Name</th>
            </tr>
        </thead>
    </table>
    <script>
    $(document).ready(function() {
        $('#example').DataTable( {
            "processing": true,
            "serverSide": true,
            "ajax": "scripts/server_processing.php"
        } );
    } );
    </script>

scripts/server_processing.php

    <?php
    // DB table to use
    $table = '$var';

    // Table's primary key
    $primaryKey = 'id';

    // indexes
    $columns = array(
        array( 'db' => 'first_name', 'dt' => 0 ),
        array( 'db' => 'last_name',  'dt' => 1 ),
        array( 'db' => 'position',   'dt' => 2 )
    );

    // SQL server connection information
    $sql_details = array(
        'user' => 'root',
        'pass' => '',
        'db'   => 'sampledb',
        'host' => 'localhost'
    );

    require( 'ssp.class.php' );

    echo json_encode(
        SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
    );

variable table-id is undefined. Is there a way to do this? The link part is important so I cannot remove that from the code.

iVirusPro
  • 37
  • 1
  • 4
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – gre_gor Sep 13 '19 at 17:52

1 Answers1

1

You need to pass the value of $_GET['table-id'] through to the AJAX call by using the ajax.data feature.

The ajax.data option provides the ability to add additional data to the request, or to modify the data object being submitted if required.

$('#example').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": {
        "url": "scripts/server_processing.php",
        "data": {
            "table_id": <?= $_GET['table_id'] ?>
        }
    }
} );

Then $_GET['table_id'] should be available within scripts/server_processing.php.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • Thanks. I enclosed php tag inside double quotes since I'm getting Syntax Error. `"table_id": "= $_GET['table_id'] ?>" `. – iVirusPro Sep 14 '19 at 12:30