我有一个名为myfunctions.PHP的文件,其中有很多功能,例如
function sendForm(){
//save form
}
function fn2(){
//do something
}
// Other functions ...
和jQuery代码,
$.ajax({
url: "myfunctions.PHP",
type: "POST",
contentType: "application/x-www-form-urlencoded",
data: {key1: "value1", key2: "value2", key3: "value3"},
complete: function(){
//completado
alert("complete");
}
});
我需要在该文件中调用特定的函数;例如sendForm().我怎样才能做到这一点?
解决方法:
在PHP中
<?PHP
// create a list of approved function calls
$approved_functions = array('sendForm','fn2');
// check the $_GET['function'] and see if it matches an approved function
if(in_array($_GET['function'], $approved_functions))
{
// call the approved function
$_GET['function']();
}
function sendForm(){
//save form
}
function fn2(){
//do something
}
在AJAX中
// specify which function to call
url: "myfunctions.PHP?function=sendForm",