使用 Jquery 调用带参数的 php 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19457612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
call php function with arguments using Jquery
提问by Tarik Mokafih
I have a php file func.php
where I defined many functions let's say :
我有一个 php 文件func.php
,我在其中定义了许多函数,比如说:
<? php
function func1($data){
return $data+1;
}
?>
I want to call the function func1
using ajax. thank you for your help
我想调用function func1
using ajax。感谢您的帮助
回答by Steve
You can't call a PHP function directly from an AJAX call, but you can do this:
您不能直接从 AJAX 调用中调用 PHP 函数,但您可以这样做:
PHP:
PHP:
<? php
function func1($data){
return $data+1;
}
if (isset($_POST['callFunc1'])) {
echo func1($_POST['callFunc1']);
}
?>
JS:
JS:
$.ajax({
url: 'myFunctions.php',
type: 'post',
data: { "callFunc1": "1"},
success: function(response) { alert(response); }
});
回答by Cristian Bitoi
You should call your php script through an ajax request, using jQuery like:
您应该使用 jQuery 通过 ajax 请求调用您的 php 脚本,例如:
Javascript:
Javascript:
$.ajax({
url: "script.php",
data: { param1: "value1", param2: "value2" },
type: "GET",
context: document.body
}).done(function() {
// your code goes here
});
You could give your parameters through data property of ajax object.
您可以通过 ajax 对象的 data 属性提供参数。
Php
php
// you can do isset check before
$param1 = $_GET['param1'];
$param2 = $_GET['param2'];
// validate // sanitize // save to db // blah blah // do something with params
More information you could get from jQuery.ajax() function description from http://api.jquery.com/jQuery.ajax/
您可以从http://api.jquery.com/jQuery.ajax/ 的jQuery.ajax() 函数描述中获得更多信息
回答by geomagas
It's a bit more complicated, but I will try to shrink it down to the basics.
它有点复杂,但我会尽量将其缩小到基础。
You will need some kind of interface (a convention, basically) between the caller and your script. Two are the main concerns here:
您将需要调用者和脚本之间的某种接口(基本上是约定)。这里有两个主要问题:
The way your script understands what needs to be called and with what arguments. Let's say you decide to use a
GET
request for calling a function. The function name could be in afunc
field and the arguments in anargs
one, as a string separated by;
. In other words, callingfuncFoo(5,'bar')
would be done by requestingfunc.php?func=func1&args=5;bar
.The way in which the caller receives (and understands) the return value. Since your requirements are js-oriented, a JSON approach would be highly appropriate.
您的脚本了解需要调用什么以及使用什么参数的方式。假设您决定使用
GET
调用函数的请求。函数名称可以在一个func
字段中,参数可以在一个字段中args
,作为由;
. 换句话说,调用funcFoo(5,'bar')
将通过请求来完成func.php?func=func1&args=5;bar
。调用者接收(并理解)返回值的方式。由于您的需求是面向 js 的,因此 JSON 方法将非常合适。
Add the following code along with your functions:
将以下代码与您的函数一起添加:
if(isset($_GET['func']))
{
$func=$_GET['func'];
if(function_exists($func))
{
$args=(isset($_GET['args'])?explode(';',$_GET['args']):array());
$result=call_user_func_array($func,$args);
}
else
$result=array('error'=>"Unknown Function $func!");
}
else
$result=array('error'=>"No function name provided!");
echo json_encode($result);
However, your functions should also be changed to meet the new requirements. Since there's no way of telling how many arguments the caller will supply, a function should be designed to accept no mandatory arguments and check for the supplied ones itself. Also, it should always return an array in order to be json_encode
d before it is returned to the caller.
但是,您的功能也应该更改以满足新的要求。由于无法知道调用者将提供多少个参数,因此函数应设计为不接受强制参数并检查提供的参数本身。此外,它应该始终返回一个数组,以便json_encode
在返回给调用者之前成为d。
Your example function, for instance, should look something like this:
例如,您的示例函数应如下所示:
function func1(){
$data=func_get_args();
if(count($data)) // at least one -- rest will be ignored
return $data[0]+1;
else
return array('error'=>__FUNCTION__."() expects an argument!");
}
Be aware though: This is just a skeleton to get you started with the whole concept. A lot more care should be taken for both fault toleranceand security. But that's another topic's subject.
但请注意:这只是让您开始了解整个概念的骨架。应更加注意容错和安全性。但那是另一个话题。
回答by navotera
Yes you can !
是的你可以 !
here my code :
这是我的代码:
$.ajax({
type: "POST",
url: "ajax.php",
data: { kode: $(this).val(), func: 'my_func' },
success: function(response) {
//any success method here
}
});
and here the code in php to receive what function to call.
这里是php中的代码来接收要调用的函数。
$post = (object) $_POST;
if(!$post)
return false;
$func = $post->func;
return $func($post);
function my_func($post) {
$json['any_data'] = false;
if($post->kode == 'ND'){
$json['any_data'] = 'ND-'.date('Y');
}
echo json_encode($json);
}
Hope it help you out bro... :D
希望它帮助你兄弟... :D