Javascript 如何正确调用PHP函数?
我目前有一个PHP函数,我需要从另一个文件调用它来填充Javascript变量。我可以很好地填充Javascript变量。然而,当调用PHP函数时,我收到一条错误消息,指出 使用未定义的常量GetTestGrid-在中假定为“GetTestGrid” 第29行的phpcom/db/estimatesCom.php 这是我的包含文件,它具有以下功能:Javascript 如何正确调用PHP函数?,javascript,php,html,function,Javascript,Php,Html,Function,我目前有一个PHP函数,我需要从另一个文件调用它来填充Javascript变量。我可以很好地填充Javascript变量。然而,当调用PHP函数时,我收到一条错误消息,指出 使用未定义的常量GetTestGrid-在中假定为“GetTestGrid” 第29行的phpcom/db/estimatesCom.php 这是我的包含文件,它具有以下功能: <?php include 'phpcom/db/estimatesCom.php';?> <?php echo getEstG
<?php include 'phpcom/db/estimatesCom.php';?>
<?php echo getEstGrid($resultsE); ?>
return getEstGrid;//<--- Return the created fucntion.
下面是我调用函数的地方:
<?php include 'phpcom/db/estimatesCom.php';?>
<?php echo getEstGrid($resultsE); ?>
return getEstGrid;//<--- Return the created fucntion.
功能代码:
function getEstGrid($resultsE){//<--This is the variable passed to the function
if ($resultsE->num_rows > 0) { //<--- Check to see if the variable is greater than zero
$rows = array(); //<--- Create an empty array calls "rows"
while ($r = $resultsE->fetch_assoc()){//<--- While the database records are being returned create a variable called "r" and assign DB record.
$rows[] = $r; //<-- assign each DB record row to the array.
}
$data = array('Estimates' => $rows);//<--- Create a variable an assing the array to it.
}
//header("Content-Type: application/json;charset=utf-8");
echo json_encode($data, true);//<--- Endode the "data" variable to a JSON format.
return getEstGrid;//<--- Return the created fucntion.
}
函数getEstGrid($resultsE){//num\u rows>0{//只需从函数中删除此返回语句:
<?php include 'phpcom/db/estimatesCom.php';?>
<?php echo getEstGrid($resultsE); ?>
return getEstGrid;//<--- Return the created fucntion.
return getEstGrid;//在另一个文件中使用函数不必返回函数
相反,只需从函数返回所需的值:
返回json_encode(
当您尝试返回“getEstGrid”时,php正在查找一个不存在的同名常量。我将更改此设置:
echo json_encode($data, true);//<--- Endode the "data" variable to a JSON format.
return getEstGrid;//<--- Return the created fucntion.
通过这种方式,函数只返回数据,您可以选择是否立即回显它或使用它执行任何其他操作。函数定义是什么样子的?看起来您没有名为getEastGrid()的函数。
。那么在estimatesCom.php中的第29行是什么?不,请看没有getEastGrid()的问题;但是有“getEstGrid()”。我认为您需要将代码包含在estimatesCom.php中以解决您的问题。在修复返回json_encode($data,true)的函数后,使用该函数可以工作;