<html>
<head>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<script>
/*
javascript通过Ajax直接调用任意PHP函数多参数例程
菜农在网友(QQ:20345037)指点下完成此例程,非常感谢!!!
雁塔菜农HotPower@163.com 2018.6.20于西安雁塔菜地
测试网址:http://www.hotpage.com.cn/phptest/HotFunction.html
*/
function GetXmlHttpObject()
{
var xmlhttp = null;
try{
xmlhttp = new XMLHttpRequest();
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxm12.XMLHTTP");
}
catch(e){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlhttp;
}
function showHint(fname, fage) {
if (fname == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = GetXmlHttpObject();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var str = this.responseText;
if(str.indexOf("HotFunction.php") > 0 && str.indexOf(fname + "()") > 0){
str = "请正确输入" + fname + "()函数的参数!!!";
}
document.getElementById("txtHint").innerHTML = str;
}
};
if(fage == ""){
xmlhttp.open("GET", "HotFunction.php?function=" + fname, true);
}
else{
xmlhttp.open("GET", "HotFunction.php?function=" + fname + "&age=" + fage, true);
}
xmlhttp.send();
}
}
</script>
</head>
<body>
<p><b>请在输入PHP函数名称及参数,Ajax返回到结果编辑框内:</b></p>
<form>
PHP函数名称: <input type="text" size="16" maxlength="" id="PHPFunctionName" value=""><br>
PHP函数参数: <input type="text" size="16" maxlength="" id="PHPFunctionAge" value=""><br>
Ajax返回结果: <span id="txtHint"></span><br>
<input type="button" onClick="showHint(PHPFunctionName.value, PHPFunctionAge.value)" value="调用PHP函数">
<p><b>javascript通过Ajax直接调用任意PHP函数多参数例程</b></p>
<p><b>菜农在网友(QQ:20345037)指点下完成此例程,非常感谢!!!</b></p>
<p><b>雁塔菜农HotPower@163.com 2018.6.20于西安雁塔菜地</b></p>
<p><b>测试网址:http://www.hotpage.com.cn/phptest/HotFunction.html</b></p>
</form>
</body>
</html>
<?php
ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);
/*
javascript通过Ajax直接调用任意PHP函数多参数例程
菜农在网友(QQ:20345037)指点下完成此例程,非常感谢!!!
雁塔菜农HotPower@163.com 2018.6.20于西安雁塔菜地
测试网址:http://www.hotpage.com.cn/phptest/HotFunction.html
*/
function f0(){
return;
}
function f1(){
return 1;
}
function f2(){
return 2;
}
function a1($a){
return $a;
}
function a2($a, $b){
return $a.$b;
}
function a3($a, $b, $c){
return $a.$b.$c;
}
function a4($a, $b, $c, $d){
return $a.$b.$c.$d;
}
$arr = array("f0", "f1", "f2", "a1", "a2", "a3", "a4");
$func = $_REQUEST["function"];
//if(function_exists($func)){//可以调用任意PHP函数
if(in_array($func,$arr)){//只能调用例程函数
$fs =isset($_REQUEST["age"])? explode(",",$_REQUEST["age"]) : array();//参数以","分割
echo call_user_func_array($func,$fs);
}else{
echo "函数$func()不存在!!!";
}
?>