【问题标题】:Notice: Undefined index: draw using Datatables.net server-side注意:未定义索引:使用 Datatables.net 服务器端绘制
【发布时间】:2018-08-17 02:07:34
【问题描述】:

错误信息: 注意:未定义的索引:draw。

另外,我的 JSON 响应出现错误:{"draw":0,"recordsTotal":23,"recordsFiltered":23,"data":[]}

....draw(上图)应该是 1 而不是 0。

代码:

$(document).ready(function() {
var asc = true;
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "server.php",
"type": "POST",
},


columnDefs: [{
targets: -1,
defaultContent: '<button type="button">Delete</button>'
}],
rowGroup: {
dataSrc: 1
}
});
});
 </script>
   <body>

 <table id="example" class="display" style="width:100%" class="table table-striped table-bordered table-hover table-condensed">
  <thead class="thead-inverse">
 <tr>
 <th> ID </th>
 <th>First Name </th>
 <th>Last Name </th>
 <th>Position </th>
 <th>Date </th>
<th>Updated </th>
 <th>Action</th>
 </thead> 
 </tr>
         <tbody>

         </tbody>
     </table>
     </div>         
 <?php

 $data=array();
 $requestData= $_REQUEST;

 $count=mysqli_query($con, "SELECT * FROM employees");
 $totalData= $count->num_rows;
 $totalFiltered=$totalData;

 $json_data = array(
            "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
            "recordsTotal"    => intval( $totalData ),  
            "recordsFiltered" => intval( $totalFiltered ), 
            "data"            => $data   // total data array
            );

 echo json_encode($json_data);
 ?>
 </script>
   <body>

 <?php
 $data=array();
 $requestData= $_REQUEST;
 $query=mysqli_query($con, "SELECT * FROM employees");
 $totalData= $count->num_rows;
 $totalFiltered=$totalData;

 if( !empty($requestData['search']['value']) ) {
    // if there is a search parameter
    $sql = "SELECT first_name, last_name, position, date, updated";
    $sql.=" FROM employees";
    $sql.=" WHERE first_name LIKE '".$requestData['search']['value']."%' ";
    // $requestData['search']['value'] contains search parameter
    $sql.=" OR last_name LIKE '".$requestData['search']['value']."%' ";
     $sql.=" OR position LIKE '".$requestData['search']['value']."%' ";
      $sql.=" OR date LIKE '".$requestData['search']['value']."%' ";
       $sql.=" OR updated LIKE '".$requestData['search']['value']."%' ";

    $query=mysqli_query($con, $sql);
    $totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result without limit in the query

    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   "; // $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc , $requestData['start'] contains start row number ,$requestData['length'] contains limit length.
    $query=mysqli_query($con, $sql); // again run query with limit

} else {   

    $sql = "SELECT first_name, last_name, position, date, updated";
    $sql.=" FROM employees";
    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
    $query=mysqli_query($con, $sql);

}

$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array();

    $nestedData[] = $row["titulo"];
    $nestedData[] = $row["descripcion"];

    $data[] = $nestedData;
}

 ?>

很有可能我不会得到答案。但觉得值得一试。我仍在等待来自 datatables.net 的回复。谢谢。

Server.php 文件:

 <?php
    $table = 'employees';
    $primaryKey = 'id'; // Table's primary key

    $columns = array(
        array( 'db' => 'id', 'dt' => 0 ),
        array( 'db' => 'first_name', 'dt' => 1 ),
        array( 'db' => 'last_name',  'dt' => 2 ),
        array( 'db' => 'position',   'dt' => 3 ),
        array( 'db' => 'date',     'dt' => 4 ),
         array( 'db' => 'updated',     'dt' => 5 ),
    );

    $sql_details = array(
        'user' => 'username',
        'pass' => 'password',
        'db'   => 'database',
        'host' => 'localhost'
    );

    require( 'ssp.class.php' );

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

【问题讨论】:

  • 那些只是警告,你可以让他们沉默访问这个答案stackoverflow.com/questions/19938003/… 但你为什么不传递任何 $_GET 数据?当您的代码需要时
  • 你指的是这个吗? "draw" => intval( $requestData['draw'] ) 应该是 requestData 没有得到。我正在尝试两种方式。我删除了 get['draw']。
  • 是的,错误似乎很小,因为整个数据表仍然有效。我只是好奇我是否错过了触发 draw 为 0 而不是 1 的东西。
  • 这是一个 PHP 通知,Datatables 只是传递它。意思是您的 draw 数组元素不存在。您可以使用isset()array_key_exists() 检查它是否存在,并在那里处理它,但这并不能真正解决您的问题(我怀疑您得到一个零值,因为索引不存在)。你有没有进入 PHP 并在那里转储记录以查看其中的实际内容?
  • 好的。有人发布了与您的评论类似的答案。我正在集思广益解决问题的几种方法..

标签: javascript json ajax datatable


【解决方案1】:

您的问题之一是您尝试访问的变量未定义。您正在尝试访问您的“绘制”变量,但您实际上并没有在任何地方定义它。

我看到您尝试通过$requestData= $_REQUEST;"draw" =&gt; intval( $requestData['draw'] ) 访问它,但$_REQUEST 只获取当前页面的$_GET 和$_POST 变量。在这里阅读更多:http://php.net/manual/en/reserved.variables.request.php

您需要向 server.php 发出单独的请求以获取这些值。

你的draw是0是因为intval(null)是0。(一个未定义的变量为null)

【讨论】:

  • 好的。我对 REQEST ​​有点怀疑。我试着把 $_REQEST ​​= 1;只是想看看会发生什么。没有什么太大的变化。解决方案大概在这个链接:datatables.net/manual/server-side
  • ""此对象响应的绘图计数器 - 来自作为数据请求的一部分发送的绘图参数。请注意,出于安全原因,强烈建议将此参数转换为整数,而不是简单地将它在 draw 参数中发送的内容回显给客户端,以防止跨站点脚本 (XSS) 攻击。”
  • 看来我需要将整数 (1?) 放入其中?
  • Draw 的结果是从 server.php 生成的。你有那个文件吗?
  • 刚刚在我的 server.php 底部进行了编辑。我注意到我的 server.php 中没有任何与绘图相关的内容....你也需要查看 ssp.class 文件吗?
【解决方案2】:

答案:将 server.php 文件中的 $_get 更改为 $_post。

这是我从 datatables.net 得到的回复。上面的帖子也与此响应有关:

“您的服务器脚本正在返回绘制值。SSP 文档指出您的脚本应该使用此参数做什么: https://datatables.net/manual/server-side#Returned-data

每个绘制请求的绘制参数递增。文档的这一部分解释了它的用途: https://datatables.net/manual/server-side#Sent-parameters

dataSrc 选项不影响绘制值。"

当我们完全解决这个问题时,我将编辑完整的答案一次。但这是目前的一般答案......

【讨论】:

    【解决方案3】:

    使用isset();

    如下图

        $draw = isset($postData['draw']);
        $start = isset($postData['start']);
        $rowperpage = isset($postData['length']); // Rows display per page
        $columnIndex = isset($postData['order'][0]['column']); // Column index
        $columnName = isset($postData['columns'][$columnIndex]['data']); // Column name
        $columnSortOrder = isset($postData['order'][0]['dir']); // asc or desc
        $searchValue = isset($postData['search']['value']); // Search value
    

    注意:未定义的索引:draw。会解决的

    【讨论】:

      猜你喜欢
      • 2011-10-30
      • 1970-01-01
      • 2015-12-01
      • 2013-11-27
      • 2015-06-02
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多