我想我需要第二雙眼睛。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);
?>
任何想法?謝謝。
您可能需要考慮傳遞$ response來連接,然後返回它而不是實現global關鍵字。 – thescientist