AloHA_ChiCken
AloHA_ChiCken

Reputation: 482

AJAX DataTable Server Side encountered Error when changing serverSide from true to false

Both these work well IF I set serverSide equal to false and using ajax to post my data as you can see below. Basically I have few dataTable which I would like to reuse the server_side.php in order to fetch data from particular tables (so i need to pass value accordingly in order for the server side). I not sure why it showing error when serverSide is set to be true while posting data to my server_side.php. While setting the serverSide = true, all I see is the client side showing processing in my DataTable. Nothing more nothing less.But if I set serverSide = false, data can be generated flawlessly, everything works fine. In my case I need to use serverSide which is true.

This is my php page which i used to display data passed from server side. The table

<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/dt-1.10.21/r-2.2.4/datatables.min.js"></script>
<script type="text/javascript">
        $(document).ready(function() {

            var example = $('#example').DataTable({
                "processing": true,
                "serverSide": true,
                "responsive": true,
                ajax: {
                    url: "server_side.php",
                    type: "post",
                    data: {
                        view: 'product_upload'
                    }
                },
                "order": [],
                "columnDefs": [{
                    targets: [0],
                    className: "dt-body-center",
                    "orderable": false,
                }, {
                    targets: [1],
                    className: "dt-body-center"
                }]
            });
</script>

This is my server_side.php.

<?php
session_start();
$whereAll        = isset($_SESSION["agent_code"]) ? ($_SESSION["agent_code"]) : '';
$view_type       = isset($_POST['view']) ? $_POST['view'] : '';
/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simple to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Easy set variables
 */

// DB table to use
$table = $view_type;


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

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array(
        'db'        => 'product_name',
        'dt'        => 0,
        'formatter' => function ($d, $row) {
            return '<input type="checkbox" id="' . $d . '" class="check_boxes">';
        }
    ),
    array(
        'db'        => 'image_path',
        'dt'        => 1,
        'formatter' => function ($d, $row) {
            return "<img src='" . $d . "' style='width:100px' class='text-center' />";
        }
    ),
    array(
        'db' => 'product_name', 'dt' => 2,
        'formatter' => function ($d, $row) {
            return 'Category :' . $row['category'] . '<br/>Name :' . $row['product_name'] . '<br/>Description :' . $row['product_description'];
        }
    ),
    array('db' => 'quantity', 'dt' => 3),
    array('db' => 'weight', 'dt' => 4),
    array('db' => 'uom', 'dt' => 5),
    array('db' => 'price', 'dt' => 6),
    array(
        'db' => 'product_name', 'dt' => 7,
        'formatter' => function ($d, $row) {
            return '<a href="#">Edit</a><br/><a href="#">Update</a><br/><a href="#">Delete</a>';
        }
    ),
    array('db' => 'category', 'dt' => 8),
    array('db' => 'product_description', 'dt' => 9)
);

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


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */

require('ssp.class.php');

echo json_encode(
    //SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns)
    SSP::complex($_GET, $sql_details, $table, $primaryKey, $columns, null, "added_by = '" . $whereAll . "' and is_show = '1'")

);

Thank you in advance!

Upvotes: 0

Views: 1113

Answers (1)

AloHA_ChiCken
AloHA_ChiCken

Reputation: 482

Downgrading DataTable version from 1.10.21 to 1.10.20 fixed my problem.

https://cdn.datatables.net/v/bs4/dt-1.10.21/r-2.2.4/datatables.min.js

https://cdn.datatables.net/v/bs4/dt-1.10.20/r-2.2.4/datatables.min.js

Not sure it is internal error or what.

Upvotes: 0

Related Questions