2017-04-27 57 views
-1

我是ajax的新手,在使用它執行表時遇到一些錯誤。此表具有搜索,排序和分頁功能。在ajax中未定義的索引

function ajaxAction(action) 
     { 
      data = $("#frm_"+action).serializeArray(); 
      $.ajax({ 
       type: "POST", 
       url: "function.php", 
       data: data , 
       dataType: "json",  
       success: function(response) 
       { 
       $('#'+action+'_model').modal('hide'); 
       $("#employee_grid").bootgrid('reload'); 
       } 
      }); 
     } 

波紋管是使用此代碼後,我的function.php代碼

<?php 

include_once("connection.php"); 
session_start(); 

$db = new dbObj(); 
$connString = $db->getConnstring(); 

$params = $_REQUEST; 

$action = isset($params['action']) != '' ? $params['action'] : ''; 
$empCls = new cusinfo($connString); 

switch($action) 
{ 
default: 
$empCls->getEmployees($params); 
return; 
} 

class cusinfo 
{ 
protected $conn; 
protected $data = array(); 
function __construct($connString) 
{ 
    $this->conn = $connString; 
} 

public function getEmployees($params) 
{ 
    $this->data = $this->getRecords($params); 
    echo json_encode($this->data); 
} 



function getRecords($params) { 

    $rp = isset($params['rowCount']) ? $params['rowCount'] : 10; 

    if (isset($params['current'])) { $page = $params['current']; } else { $page=1; }; 

    $start_from = ($page-1) * $rp; 

    $sql = $sqlRec = $sqlTot = $where = ''; 

    if(!empty($params['searchPhrase'])) 
    { 
     $where .=" WHERE "; 
     $where .=" (NO_ID LIKE '".$params['searchPhrase']."%' ";  
     $where .=" OR TICKET_ID LIKE '".$params['searchPhrase']."%' "; 
     $where .=" OR CAT_C_B LIKE '".$params['searchPhrase']."%' "; 
     $where .=" OR ORDER_TYPE LIKE '".$params['searchPhrase']."%' "; 
     $where .=" OR RECEIVED_DATE LIKE '".$params['searchPhrase']."%' "; 
     $where .=" OR AGENT_STAFF_NAME LIKE '".$params['searchPhrase']."%' "; 
     $where .=" OR EFORM_ID LIKE '".$params['searchPhrase']."%' "; 
     $where .=" OR LATEST_ORDER_STATUS LIKE '".$params['searchPhrase']."%')"; 
    } 
    if(!empty($params['sort'])) 
    { 
     $where .=" ORDER By ".key($params['sort']) .' '.current($params['sort'])." "; 
    } 
    // getting total number records without any search 

    $tid = $_SESSION['sess_user_id']; 
    $sql = "SELECT * FROM `cusinfo` where AGENT_CODE_STAFF_ID = '$tid' "; 
    $sqlTot .= $sql; 
    $sqlRec .= $sql; 

    //concatenate search sql if value exist 
    if(isset($where) && $where != '') 
    { 
     $sqlTot .= $where; 
     $sqlRec .= $where; 
    } 
    if ($rp!=-1) 
    $sqlRec .= " LIMIT ". $start_from .",".$rp; 

    $qtot = mysqli_query($this->conn, $sqlTot) or die("error to fetch tot customer data"); 
    $queryRecords = mysqli_query($this->conn, $sqlRec) or die("error to fetch customer data"); 

    while($row = mysqli_fetch_assoc($queryRecords)) 
    { 
     $data[] = $row; 
    } 

    $json_data = array(
     "current"  => intval($params['current']), 
     "rowCount"  => 10,   
     "total"   => intval($qtot->num_rows), 
     "rows"   => $data // total data array 
     ); 

    return $json_data; 
} 


} 
?> 

,我的搜索功能不能work.And我得到的通知

Notice: Undefined index: current in C:\xxx\xxx\xxxx\function.php on line 92 
+0

你可以在代碼中標出92行嗎? –

+0

「current」=> intval($ params ['current']),.這個代碼在 $ json_data = array –

+0

@ C.Norris猜它是在這裏'intval($ params ['current']),' –

回答

1

既然你給了我92行的代碼,

這可能幫助..

你已經沒有條件語句

if (isset($params['current'])) { $page = $params['current']; } else { $page=1; }; 

所以,你應該再次使用,在你的其他代碼 $

json_data = array(
     "current"  => $page, 
     "rowCount"  => 10,   
     "total"   => intval($qtot->num_rows), 
     "rows"   => $data // total data array 
); 
+0

後,搜索功能無法正常工作,該通知實際上不再顯示,但我的搜索功能仍然無法工作。 –

1

你還沒有驗證$params['current']被定義爲。

if (isset($params['current'])) { 
    $page = $params['current']; 
} else { 
    $page=1; 
}; 

但如果$params['current']未設置發生了什麼:在這一行,如果$params['current']設置要檢查?在第92行中,您總是使用$params['current'],不管它是否已定義,所以當未定義$params['current']時,您將得到未定義索引錯誤。您必須在使用此索引之前進行檢查。

1

出現current未作爲您的ajax請求中的參數傳遞。

你可以通過一個簡短的句柄來處理這個異常,如果語句不存在,並且設置了默認值。像:

$json_data = array(
     "current"  => isset($params['current'])?intval($params['current']):1;, 
     "rowCount"  => 10,   
     "total"   => intval($qtot->num_rows), 
     "rows"   => $data // total data array 
    ); 

PHP 7介紹了空聚結運營商甚至進一步簡化了這一說法,如果第一個參數不爲空,將使用其他第二放慢參數將作爲默認值。像這樣:

$json_data = array(
    "current"  => intval($params['current']) ?? 1 
    "rowCount"  => 10,   
    "total"   => intval($qtot->num_rows), 
    "rows"   => $data // total data array 
); 

這是一個後端解決方案,用於處理未設置電流的情況。我還要檢查你的Ajax數據正在準備看看爲什麼它沒有被列入請求

+0

我已經改變了你的代碼,通知不再顯示。但我的搜索功能仍然無法正常工作。添加會話[tmid]到代碼 –