这可能是一个简单的答案,但我使用jQuery的$ .ajax来调用PHP脚本.我想要做的基本上是将PHP脚本放在一个函数中并从javascript调用PHP函数.
<?php
if(isset($_POST['something'] {
//do something
}
?>
Run Code Online (Sandbox Code Playgroud)
对此
<?php
function test() {
if(isset($_POST['something'] {
//do something.
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我如何在javascript中调用该函数?现在我只使用$ .ajax列出的PHP文件.
kar*_*m79 227
使用$.ajax
调用服务器上下文(或URL,或其他)来调用特定的"动作".你想要的是:
$.ajax({ url: '/my/site',
data: {action: 'test'},
type: 'post',
success: function(output) {
alert(output);
}
});
Run Code Online (Sandbox Code Playgroud)
在服务器端,action
应该读取POST参数,相应的值应该指向要调用的方法,例如:
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'test' : test();break;
case 'blah' : blah();break;
// ...etc...
}
}
Run Code Online (Sandbox Code Playgroud)
我相信这是Command模式的简单化身.
Xax*_*xis 12
我开发了一个jQuery插件,允许你调用任何核心PHP函数甚至用户定义的PHP函数作为插件的方法:jquery.php
在我们的文档头部包含jquery和jquery.php并将request_handler.php放在我们的服务器上之后,我们将以下面描述的方式开始使用插件.
为便于使用,请以简单的方式引用该功能:
var P = $.fn.php;
Run Code Online (Sandbox Code Playgroud)
然后初始化插件:
P('init',
{
// The path to our function request handler is absolutely required
'path': 'http://www.YourDomain.com/jqueryphp/request_handler.php',
// Synchronous requests are required for method chaining functionality
'async': false,
// List any user defined functions in the manner prescribed here
// There must be user defined functions with these same names in your PHP
'userFunctions': {
languageFunctions: 'someFunc1 someFunc2'
}
});
Run Code Online (Sandbox Code Playgroud)
现在一些使用场景:
// Suspend callback mode so we don't work with the DOM
P.callback(false);
// Both .end() and .data return data to variables
var strLenA = P.strlen('some string').end();
var strLenB = P.strlen('another string').end();
var totalStrLen = strLenA + strLenB;
console.log( totalStrLen ); // 25
// .data Returns data in an array
var data1 = P.crypt("Some Crypt String").data();
console.log( data1 ); // ["$1$Tk1b01rk$shTKSqDslatUSRV3WdlnI/"]
Run Code Online (Sandbox Code Playgroud)
演示PHP函数链:
var data1 = P.strtoupper("u,p,p,e,r,c,a,s,e").strstr([], "C,A,S,E").explode(",", [], 2).data();
var data2 = P.strtoupper("u,p,p,e,r,c,a,s,e").strstr([], "C,A,S,E").explode(",", [], 2).end();
console.log( data1, data2 );
Run Code Online (Sandbox Code Playgroud)
演示发送PHP伪代码的JSON块:
var data1 =
P.block({
$str: "Let's use PHP's file_get_contents()!",
$opts:
[
{
http: {
method: "GET",
header: "Accept-language: en\r\n" +
"Cookie: foo=bar\r\n"
}
}
],
$context:
{
stream_context_create: ['$opts']
},
$contents:
{
file_get_contents: ['http://www.github.com/', false, '$context']
},
$html:
{
htmlentities: ['$contents']
}
}).data();
console.log( data1 );
Run Code Online (Sandbox Code Playgroud)
后端配置提供白名单,因此您可以限制可以调用的功能.还有一些其他模式可以使用插件描述的PHP.
我会坚持使用常规方法直接调用文件,但是如果您确实要调用函数,请查看JSON-RPC(JSON远程过程调用)。
您基本上是将特定格式的JSON字符串发送到服务器,例如
{ "method": "echo", "params": ["Hello JSON-RPC"], "id": 1}
Run Code Online (Sandbox Code Playgroud)
其中包括要调用的函数以及该函数的参数。
当然,服务器必须知道如何处理此类请求。
这是JSON-RPC的jQuery插件,例如Zend JSON Server作为PHP中的服务器实现。
对于较小的项目或功能较少的项目,这可能是过大的选择。最简单的方法就是karim的答案。另一方面,JSON-RPC是标准。