使用Jquery和Ajax从PHP调用特定函数。

时间:2022-12-07 23:59:33

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脚本中看到的,有两个函数,我正在尝试使用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?

现在如果我有一个函数,我就可以做,但当我有两个或更多的函数时,我就开始卡住了。我想当我有两个函数时,我可以这样做,但是只要有更多的变量或者更多的函数我就可以在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>

3 个解决方案

#1


11  

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
}

#2


3  

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。例如,您传递的请求=保存在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

编辑:你甚至可以和其他参数一起传递。

#3


0  

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

}

#1


11  

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
}

#2


3  

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。例如,您传递的请求=保存在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

编辑:你甚至可以和其他参数一起传递。

#3


0  

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

}