jackhammer013
jackhammer013

Reputation: 2295

Undefined index: sSearch jQuery Datatables Laravel 5.1

EDIT As some answer suggests, I am aware that this error occurs because the variable is non existent and is not passed. Question is how do I configure this for Datatables, I believe thos variable should came from my Ajax code as variable but how do I set it or what is the proper format in codes for it to work?

I am working on jQuery Datatables library using Postgre SQL and following this link.

https://datatables.net/development/server-side/php_postgres

I followed is correctly and I believe that the database connection is good.

But I am getting this error:

Undefined index: sSearch

I am using Laravel 5.1 and I have a view which I fetch data via Ajax then populating it in jQuery datatables. I used the server side method: My code:

public function apiGetCustomers()
{

    $aColumns = array( 'id', 'firstname', 'lastname', 'gender', 'phone_num', 'country', 'postcode' );

    $sIndexColumn = "id";

    $sTable = "customers";

    $gaSql['user']       = "postgres";
    $gaSql['password']   = "postgres";
    $gaSql['db']         = "qms";
    $gaSql['server']     = "localhost";

    $gaSql['link'] = pg_connect(
        " host=".$gaSql['server'].
        " dbname=".$gaSql['db'].
        " user=".$gaSql['user'].
        " password=".$gaSql['password']
    ) or die('Could not connect: ' . pg_last_error());

    $sLimit = "";
    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
    {
        $sLimit = "LIMIT ".intval( $_GET['iDisplayLength'] )." OFFSET ".
            intval( $_GET['iDisplayStart'] );
    }

    if ( isset( $_GET['iSortCol_0'] ) )
    {
        $sOrder = "ORDER BY  ";
        for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
        {
            if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
            {
                $sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
                    ".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc').", ";
            }
        }

        $sOrder = substr_replace( $sOrder, "", -2 );
        if ( $sOrder == "ORDER BY" )
        {
            $sOrder = "";
        }
    }

    $sWhere = "";
    if ( $_GET['sSearch'] != "" )
    {
        $sWhere = "WHERE (";
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( $_GET['bSearchable_'.$i] == "true" )
            {
                $sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string( $_GET['sSearch'] )."%' OR ";
            }
        }
        $sWhere = substr_replace( $sWhere, "", -3 );
        $sWhere .= ")";
    }

    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
        {
            if ( $sWhere == "" )
            {
                $sWhere = "WHERE ";
            }
            else
            {
                $sWhere .= " AND ";
            }
            $sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string($_GET['sSearch_'.$i])."%' ";
        }
    }

    $sQuery = "
    SELECT ".str_replace(" , ", " ", implode(", ", $aColumns))."
    FROM   $sTable
    $sWhere
    $sOrder
    $sLimit
    ";
    $rResult = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());

    $sQuery = "
        SELECT $sIndexColumn
        FROM   $sTable
    ";
    $rResultTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
    $iTotal = pg_num_rows($rResultTotal);
    pg_free_result( $rResultTotal );

    if ( $sWhere != "" )
    {
        $sQuery = "
            SELECT $sIndexColumn
            FROM   $sTable
            $sWhere
        ";
        $rResultFilterTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
        $iFilteredTotal = pg_num_rows($rResultFilterTotal);
        pg_free_result( $rResultFilterTotal );
    }
    else
    {
        $iFilteredTotal = $iTotal;
    }

    $output = array(
        "sEcho" => intval($_GET['sEcho']),
        "iTotalRecords" => $iTotal,
        "iTotalDisplayRecords" => $iFilteredTotal,
        "aaData" => array()
    );

    while ( $aRow = pg_fetch_array($rResult, null, PGSQL_ASSOC) )
    {
        $row = array();
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( $aColumns[$i] == "version" )
            {
                /* Special output formatting for 'version' column */
                $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
            }
            else if ( $aColumns[$i] != ' ' )
            {
                /* General output */
                $row[] = $aRow[ $aColumns[$i] ];
            }
        }
        $output['aaData'][] = $row;
    }

     echo json_encode( $output );

    // Free resultset
    pg_free_result( $rResult );

    // Closing connection
    pg_close( $gaSql['link'] );

}

The Ajax in my view

$(document).ready(function() {
    $('#CustomerList').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "api/customer/all",
        "paging" : true,
        //"scrollY" : 400,
        "searching" : true,
        "ordering" :  true,
        //"pagingType" : "full_numbers" 
    } );
});

Upvotes: 1

Views: 1339

Answers (3)

Sam
Sam

Reputation: 920

Setting display_errors to Off in php.ini helped me. I would suggest to try it as well.

Upvotes: 0

davidkonrad
davidkonrad

Reputation: 85558

You have an 1.9.x serverside script but are using the 1.10.x naming conventions and options layout. When you have defined

serverSide: true,
ajax: "api/customer/all",

etc, you also instruct dataTables to sent params to the server in the newer format, i.e search instead of sSearch and so forth. The easiest way to overcome this issue is to force dataTables AJAX handling into legacy mode :

$.fn.dataTable.ext.legacy.ajax = true;

Upvotes: 1

cre8
cre8

Reputation: 13562

Replace

if ( $_GET['sSearch'] != "" )

with

if (isset($_GET['sSearch']) && !empty($_GET['search']))

you try to call a variable that maybe doesn't exists.

Upvotes: 0

Related Questions