Datatables server-side processing not displaying the output on-page and where condition not working

Viewed 1357

I am using data tables server-side processing with ajax. Here is the link I am referring https://datatables.net/examples/data_sources/server_side.html

I am getting issues which are as follows

1) I am getting the output on the network tab but it's not displaying on my page.

Network tab screenshot

enter image description here

On the page, It's displaying only Processing

enter image description here

2) I try to use where condition but that also not working.

I tried below code

<table id="list" class="table table-striped table-bordered">
  <thead>
    <tr class="table-column-heading">
      <th>Name</th>
      <th>Email_Id</th>
      <th>Payment id</th>
      <th>Date Of Added</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>
<script>
  $(document).ready(function() {
    var dataTable = $('#list').DataTable({
      "processing": true,
      "serverSide": true,
      "ajax": {
        url: "fetch.php",
        type: "post",
        //data: {'action': 'servicelist'}
      }
    });
  });
</script>

fetch.php

$table = 'tbl_participantdetails';
$primaryKey = 'pd_id';
$columns = array(
    array( 'db' => 'pd_name', 'dt' => 0 ),
    array( 'db' => 'pd_email',  'dt' => 1 ),
    array( 'db' => 'paid',   'dt' => 2 ),
    array( 'db' => 'payment_id', 'dt' => 3,),
    array( 'db' => 'date_of_added','dt' => 4,
        'formatter' => function( $d, $row ) {
            return date( 'd-m-Y', strtotime($d));
        }
    )
);
require( 'datatables/ssp.class.php' );
$where = "paid =1";
 
echo json_encode(
    SSP::simple( $_GET, $pdo, $table, $primaryKey, $columns,$where )
);

This is the second option I tried and it's working

<table id="list" class="table table-striped table-bordered">
  <thead>
    <tr class="table-column-heading">
      <th>Name</th>
      <th>Email_Id</th>
      <th>Paid</th>
      <th>Payment id</th>
      <th>Date Of Added</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>
$(document).ready(function() {
  var dataTable = $('#list').DataTable({
    "processing": true,
    "serverSide": true,
    "paging": true,
    "searchable": true,
    "ajax": {
      url: "fetch.php",
      type: "post"
    },
    language: {
      sLengthMenu: "Show _MENU_", // remove entries text
      searchPlaceholder: "Search",
      emptyTable: "No record found",
      search: ""
    },
    "pageLength": 25,
    "paging": true,
    "columns": [{
        "data": "pd_name"
      },
      {
        "data": "pd_email"
      },
      {
        "data": "paid"
      },
      {
        "data": "payment_id"
      },
      {
        "data": "date_of_added"
      }
    ]
  });
});

fetch.php

$stmt = $pdo->prepare("SELECT count(*) FROM  tbl_participantdetails WHERE is_active = 1 and paid=1");
$stmt->execute();
$totalRecords = $stmt->fetchColumn();
$query="SELECT `pd_id`, `pd_name`, `pd_pic`, `pd_email`, `paid`, `payment_id`, `date_of_added` FROM `tbl_participantdetails` WHERE is_active= 1 and paid=1";
try {
                  $stmt = $pdo->prepare($query);
                  $stmt->execute();
                  $result = $stmt->fetchAll();
                 
              $data['data'] = [];
              foreach ($result as $row) {
        $arr_result = array(
                    //"id" =>$i++,
                    "pd_name" =>"<img src='".$row['pd_pic']."'>'".$row['pd_name'],
                    "pd_email" => $row['pd_email'],
                    "paid" => $row['paid'],
                    "payment_id" => $row['payment_id'],
                    "date_of_added" => $row['date_of_added']
        );


                 $data['data'][] = $arr_result;
                }
                }
                catch(PDOException $e) {
                    echo "Error: " . $e->getMessage();
                }

$json_data = array(
"draw"            => intval($_REQUEST['draw'] ),   
"recordsTotal"    => intval($totalRecords ),  
"recordsFiltered" => intval($totalRecords),
"data"            => $data['data']
);
echo json_encode($json_data);
exit;
1 Answers

Try using SSP::complex() instead of SSP::simple()

And also it should be SSP::complex($_POST...) in PHP, since you are using POST type in your ajax request.

Nothing is wrong with your url properties in the ajax.

Related