Php ajax中未定义的索引
我是ajax新手,当我用它做一个表时,我会遇到一些错误。这个表有搜索、排序和分页功能Php ajax中未定义的索引,php,jquery,ajax,Php,Jquery,Ajax,我是ajax新手,当我用它做一个表时,我会遇到一些错误。这个表有搜索、排序和分页功能 function ajaxAction(action) { data = $("#frm_"+action).serializeArray(); $.ajax({ type: "POST", url: "function.php", data: da
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;
}
}
?>
您尚未验证是否定义了$params['current']。在这一行中,您将检查是否设置了$params['current']:
但是如果未设置$params['current'],会发生什么?在第92行中,无论是否定义了$params['current'],始终使用$params['current'],因此当未定义$params['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
);
PHP7引入了null coalesce运算符,它进一步简化了该语句,如果第一个参数不是空的,它将使用该参数,否则第二个参数将用作默认值。像这样:
$json_data = array(
"current" => intval($params['current']) ?? 1
"rowCount" => 10,
"total" => intval($qtot->num_rows),
"rows" => $data // total data array
);
这是一个后端解决方案,可在未设置当前值时处理。我还要检查您准备好的ajax数据,看看为什么自从您在第92行给我代码后,它就没有包含在请求中 这可能会有帮助 你已经做了一个条件语句
if (isset($params['current'])) { $page = $params['current']; } else { $page=1; };
所以你应该在其他代码中再次使用它
$
您能在您给出的代码中标记第92行的位置吗?…current=>intval$params['current'],。$json_data=array@C.Norris我猜它在这里intval$params['current'],我已经更改了与您相同的代码,通知不再显示。但是我的搜索功能仍然无法工作。在我将会话[tmid]添加到代码后,搜索功能无法工作通知确实不再显示,但我的搜索功能仍然无法工作。
$json_data = array(
"current" => intval($params['current']) ?? 1
"rowCount" => 10,
"total" => intval($qtot->num_rows),
"rows" => $data // total data array
);
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
);