使用 Jquery & Ajax 从 PHP 调用特定函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25677368/
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
Calling a specific function from PHP with Jquery & Ajax
提问by jwknz
For me this is something new, so I am just researching this and trying to understand it. As you can see in the php script there are 2 functions and I am trying to call a specific one with jquery.
对我来说,这是新事物,所以我只是在研究它并试图理解它。正如您在 php 脚本中看到的那样,有 2 个函数,我正在尝试使用 jquery 调用一个特定的函数。
Now if I have one function then I can do it, but when I have 2 or more I am starting to get stuck. I suppose I could do this when I have 2 functions, but as soon as more variables are in play or more functions do I just make massive if statements in my php?
现在,如果我有一个功能,那么我可以做到,但是当我有 2 个或更多功能时,我就开始卡住了。我想我可以在有 2 个函数时执行此操作,但是一旦有更多变量在运行或更多函数,我是否只在我的 php 中创建大量 if 语句?
The problem is that when I attach a database to it, I would need to consider all inputs that can happen. How do I specify a specific php function when using jquery & ajax?
问题是,当我将数据库附加到它时,我需要考虑所有可能发生的输入。 使用 jquery 和 ajax 时如何指定特定的 php 函数?
//function.php
<?php
function firstFunction($name)
{
echo "Hello - this is the first function";
}
function secondFunction($name)
{
echo "Now I am calling the second function";
}
?>
<?php
$var = $_POST['name'];
if(isset($var))
{
$getData = firstFunction($var);
}
else if(isset($var))
{
$getData = secondFunction($var);
}
else
{
echo "No Result";
}
?>
//index.html
<div id="calling">This text is going to change></div>
<script>
$(document).ready(function() {
$('#calling').load(function() {
$.ajax({
cache: false,
type: "POST",
url: "function.php",
data: 'name=myname'
success: function(msg)
{
$('#calling').html((msg));
}
}); // Ajax Call
}); //event handler
}); //document.ready
</script>
回答by Robbie Averill
You need to pass a parameter in, either via the data object or via a GET variable on the URL. Either:
您需要通过数据对象或通过 URL 上的 GET 变量传递参数。任何一个:
url: "function.php?action=functionname"
or:
或者:
data: {
name: 'myname',
action: 'functionname'
}
Then in PHP, you can access that attribute and handle it:
然后在 PHP 中,您可以访问该属性并对其进行处理:
if(isset($_POST['action']) && function_exists($_POST['action'])) {
$action = $_POST['action'];
$var = isset($_POST['name']) ? $_POST['name'] : null;
$getData = $action($var);
// do whatever with the result
}
Note:a better idea for security reasons would be to whitelist the available functions that can be called, e.g.:
注意:出于安全原因,一个更好的主意是将可以调用的可用函数列入白名单,例如:
switch($action) {
case 'functionOne':
case 'functionTwo':
case 'thirdOKFunction':
break;
default:
die('Access denied for this function!');
}
Implementation example:
实现示例:
// PHP:
function foo($arg1) {
return $arg1 . '123';
}
// ...
echo $action($var);
// jQuery:
data: {
name: 'bar',
action: 'foo'
},
success: function(res) {
console.log(res); // bar123
}
回答by Raptor
You are actually quite close to what you want to achieve.
你实际上非常接近你想要实现的目标。
If you want to specify which function will be called in PHP, you can pass a variable to tell PHP. For example, you passed request=save
in AJAX, you can write the PHP as follow:
如果要指定在 PHP 中将调用哪个函数,可以传递一个变量来告诉 PHP。比如你传入request=save
AJAX,你可以这样写PHP:
$request = '';
switch(trim($_POST['request'])) {
case 'save':
$player_name = (isset($_POST['playername']) ? trim($_POST['player_name']) : 'No Name'));
saveFunction($player_name);
break;
case 'load':
loadFunction();
break;
default:
// unknown / missing request
}
EDIT:You can even pass along with other parameters
编辑:您甚至可以与其他参数一起传递
回答by DragonFire
This may not be exactly what you are looking for but it can help some others looking for a very simple solution.
这可能不是您正在寻找的,但它可以帮助其他人寻找一个非常简单的解决方案。
In your jquery declare a variable and send it
在您的 jquery 中声明一个变量并发送它
var count_id = "count";
data:
{
count_id: count_id
},
Then in your php check if this variable is set
然后在您的 php 中检查是否设置了此变量
if(isset($_POST['count_id'])) {
Your function here
}