我想我需要第二双眼睛。
我有一些调用php文件的ajax,它正在返回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);
?>
有什么想法吗?感谢。
答案 0 :(得分:6)
您的$response
变量超出了scope ..在您的函数中使用global
关键字来注册您的外部变量
function connect() {
global $response;
$response['alert'] = 'connect ran';
}
或SDC的编辑:
function connect($response) {
$response['alert'] = 'connect ran';
}
connect($response);
答案 1 :(得分:0)
实际上你定义了结果变量,但是在另一种类型中,你还在顶部有另一个结果变量,所以你把数据放在$ result []中,但你试着使用$ result,所以你的代码可能不会给你预期的结果。