开发者

Access json-returning php functions from javascript

I 开发者_如何学JAVAhave a php file that creates a json output. my problem is that i will need lots of functions that return json data. For example:

  1. i will need to fetch a list of all customers in a given db;
  2. I will need to fetch all vehicles from a given customer;
  3. i will need to fetch all data from a given vehicle

and so on. At this point i can have all this working if each function is in a separate php file. The complexity of this is only going to increase and i dont want to have lots of files cluttering the file system. Is there a way around this? any practical example would be appreciated


Sure. If you have one function for each type of action, like this:

function action_hello_world() {
    echo "Hello, world!";
}
function action_hello() {
    echo "Hello, {$_GET["name"]}!";
}

Then create an array of valid actions:

$valid_actions=array("hello_world", "hello");

Then we have a GET parameter called action:

$action=$_GET["action"];

We check to make sure it's valid...

if(!in_array($action, $valid_actions)) {
    die("Invalid action.");
}

Then we call the correct function:

$function_name="action_$action";
$function_name();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜