我有一个JSON函数,它运行完美,没有任何规范要运行的函数的“哪个部分”。最初,我有一个JSON函数可以“查看”详细的行信息,还有一个JSON函数可以“编辑”详细的行信息。每个功能都是完美的。然而,在听取了同事关于如何将两种功能合并到一个脚本中的建议之后,它似乎失败了,我不明白为什么......希望SO的专家可以帮助确定它的原因失败。所有变量都被确认为成功通过AJAX。
$id = (int)$_POST['id'];
$query = "SELECT * FROM table WHERE id=$id LIMIT 1"; //expecting one row
try {
$result = $customer->runQuery($query);
$message = array(
'id' => $id,
'name' => $result[0]['agency_name'],
'account' => $result[0]['account_number'],
'phone' => $result[0]['phone']
);
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}}
header('Content-Type: application/json');
print json_encode($message);
$id = (int)$_POST['id'];
$action = $_POST['action']; //either "get" or "set"
$action(); //On loading this variable, the correct function will be run and executed
function get() {
$query = "SELECT * FROM table WHERE id=$id LIMIT 1"; //expecting one row
try {
$result = $customer->runQuery($query);
$message = array(
'id' => $id,
'name' => $result[0]['agency_name'],
'account' => $result[0]['account_number'],
'phone' => $result[0]['phone']
);
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}}
function set() { //Do Something Else}
header('Content-Type: application/json');
print json_encode($message);