PHP函数没有被调用,我错过了一些明显的东西?
问题描述:
我想我需要第二双眼睛。PHP函数没有被调用,我错过了一些明显的东西?
我有一些ajax调用一个php文件,它返回json。这一切工作正常。然后我提醒我返回的数据元素用于测试目的。在这样做的时候,我缩小了我的功能,并没有被调用。
<?php
// database functions
$response = array();
$count = 1;
// connect to db
function connect() {
$response['alert'] = 'connect ran'; // does not get alerted
}
// loop through query string
foreach ($_POST as $key => $value) {
switch ($key) {
case 'connect':
$response['alert'] = 'case ran';
if ($value == 'true') {
$response['alert'] = 'if ran'; // this is what gets alerted, should be overwriten by 'connect ran'
connect(); // function call does not work?
} else {
$response['alert'] = 'false';
$mysqli->close();
}
break;
case 'otherstuff':
break;
}
++$count;
}
$response['count'] = $count;
echo json_encode($response);
?>
任何想法?谢谢。
答
其实你定义的结果变量但是在另一种类型中,并且在顶部还有另一个结果变量,所以您将数据放在$ result []中,但是您尝试使用$ result,因此您的代码可能不会给您预期的结果。
您可能需要考虑传递$ response来连接,然后返回它而不是实现global关键字。 – thescientist