如何调用URL中的函数(PHP)

时间:2017-05-26 15:42:07

标签: javascript php

目前我的代码如下所示。 因此,当我点击按钮时,它将显示成功或失败。 我在另一个网页上有另一个PHP脚本可以从中调用。

目前我使用php文件名调用php脚本。我想检查有没有办法让我使用url中的函数调用php文件? 原因是,在PHP脚本中,我将有几个函数来调用。我不想创建多个php文件。

下面是我的代码。

<script>
     function bookBTN(x) {
       $.getJSON('http://localhost/movieOne.php?method=getSeatNum1&seatNum=' + x, function(data) { 
          if (data.avail == "yes") {
            alert("Success"); 
          }
          else { alert("Failure"); } 
        });
     }
</script>

    <script>
     function viewBTN(x) {
       $.getJSON('http://localhost/movieOne.php?method=getSeatNum2&seatNum=' + x, function(data) { 
          if (data.avail == "yes") {
            alert("Success"); 
          }
          else { alert("Failure"); } 
        });
     }
</script>

movieOne.php

  <?php
    $seatNum= $_GET["seatNum"]; 
    getSeatNum1($seatNum);
    function getSeatNum1($seatNum) {
      $seatNum = $_GET["seatNum"]; 
      $url = 'http://movie.com/movieOne?seatNum=' . $seatNum;
      $result = file_get_contents($url); 
      echo $result; 
    ?>

 <?php
    $seatNum= $_GET["seatNum"]; 
    getSeatNum2($seatNum);
    function getSeatNum2($seatNum) {
      $seatNum = $_GET["seatNum"]; 
      $url = 'http://movie.com/movieOne?seatNum=' . $seatNum;
      $result = file_get_contents($url); 
      echo $result; 
    ?>

当我只运行http://localhost/movieOne.php?method=getSeatNum1&seatNum=' + x并且在movieOne.php中只有1个php函数时,它运行正常。

当我运行http://localhost/movieOne.php?method=getSeatNum1&seatNum=' + xhttp://localhost/movieOne.php?method=getSeatNum2&seatNum=' + x时,在movieOne.php中只有1个php函数,它也能正常工作。

但是当我运行http://localhost/movieOne.php?method=getSeatNum1&seatNum=' + xhttp://localhost/movieOne.php?method=getSeatNum2&seatNum=' + x并且有2个不同的功能(如上面的代码)时,该按钮不起作用。

1 个答案:

答案 0 :(得分:0)

如果你想像codeigniter那样从url调用函数,我有一个例子

网址示例:http://localhost/jastip/ajax/request.php/get_orders

(function () {
    $url_function = explode('/', $_SERVER['REQUEST_URI']);
    $function_name = get_defined_functions()['user'];    

    if (in_array($url_function[4], $function_name)) {
        $index = array_search($url_function[4], $function_name);
        $dynamic_fun = $function_name[$index];
        $dynamic_fun();
    } else {
        var_dump("Page not found");
        die;
    }
})();    

function get_orders()
{
    echo "get orders";
}    

function get_something()
{
    echo "get something";
}