每当您通过HTTP请求(即GET,POST,PUT)访问它时,都会运行PHP文件.
您可以使用JQuery / Ajax在按钮点击上发送请求,甚至只需更改浏览器的URL即可导航到PHP地址.
根据POST / GET中发送的数据,您可以有一个运行不同功能的switch语句.
通过GET指定功能
所以在PHP方面你可以有这样的东西:
//see http://PHP.net/manual/en/function.call-user-func-array.PHP how to use extensively
if(isset($_GET['runFunction']) && function_exists($_GET['runFunction']))
call_user_func($_GET['runFunction']);
else
echo "Function not found or wrong input";
function test()
{
echo("test");
}
function hello()
{
echo("hello");
}
?>
您可以使用地址栏做最简单的获取请求作为测试:
http://127.0.0.1/test.PHP?runFunction=hellodddddd
结果是:
Function not found or wrong input
http://127.0.0.1/test.PHP?runFunction=hello
结果是:
hello
发送数据
通过JQuery获取请求
$.get("test.cgi",{ name: "John"})
.done(function(data) {
alert("Data Loaded: " + data);
});
POST请求通过JQuery
$.post("test.PHP",{ name: "John"} );
通过Javascript位置获取请求
value="insert button text here"
onClick="self.location='Your_URL_here.PHP?name=hello'">
阅读数据(PHP)
有用的链接