ajax中的未定义索引

时间:2017-04-27 05:41:29

标签: php jquery 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;
}


}
?>

使用此代码后,我的搜索功能无法正常工作。我收到通知

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

3 个答案:

答案 0 :(得分:1)

您尚未确认$params['current']已定义。在此行中,您正在检查是否设置了$params['current']

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

但如果未设置$params['current']会怎样?在第92行中,您始终使用$params['current'],无论是否已定义,因此在$params['current']未定义时,您将收到未定义索引错误。您必须在使用此索引之前进行检查。

答案 1 :(得分:1)

似乎未将current作为参数传递给您的ajax请求。

您可以通过简短的if语句处理此异常,如果不存在则设置default。像:

    $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 引入了null coalesce运算符,如果第一个参数不为空,它将进一步简化此语句,它将使用其他第二个参数将用作默认值。像这样:

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

这是一个后端解决方案,用于处理未设置电流的情况。我还会检查你准备的ajax数据,看看为什么它没有被包含在请求中

答案 2 :(得分: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
 );