简体   繁体   English

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

[英]How to call a function in the URL (PHP)

Currently I have this code as shown below.目前我有这个代码,如下所示。 Whereby, when I click on the button, it will show either success or fail.因此,当我点击按钮时,它会显示成功或失败。 I have another php script on another webpage to call from it.我在另一个网页上有另一个 php 脚本可以调用它。

Currently I call the php script, using the php file name.目前我使用 php 文件名调用 php 脚本。 I would like to check is there a way for me to call the php file using a function in the url?我想检查是否有办法使用 url 中的函数调用 php 文件? The reason is because, in the php script, I would have several functions to call from.原因是,在 php 脚本中,我将有几个函数可供调用。 I do not want to create multiple php file.我不想创建多个 php 文件。

below is my code.下面是我的代码。

<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

  <?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; 
    ?>

When I only run http://localhost/movieOne.php?method=getSeatNum1&seatNum=' + x and having only 1 php function inside movieOne.php , it works fine.当我只运行http://localhost/movieOne.php?method=getSeatNum1&seatNum=' + x并且在 movieOne.php 中只有 1 个 php 函数时,它工作正常。

When I run http://localhost/movieOne.php?method=getSeatNum1&seatNum=' + x and http://localhost/movieOne.php?method=getSeatNum2&seatNum=' + x , having only 1 php function inside movieOne.php , it works fine too.当我运行http://localhost/movieOne.php?method=getSeatNum1&seatNum=' + xhttp://localhost/movieOne.php?method=getSeatNum2&seatNum=' + xhttp://localhost/movieOne.php?method=getSeatNum2&seatNum=' + x中只有 1 个 php 函数,它工作正常。

However when I run http://localhost/movieOne.php?method=getSeatNum1&seatNum=' + x and http://localhost/movieOne.php?method=getSeatNum2&seatNum=' + x , and have 2 different function (as the code above), the button doesn't work.但是,当我运行http://localhost/movieOne.php?method=getSeatNum1&seatNum=' + xhttp://localhost/movieOne.php?method=getSeatNum2&seatNum=' + x ,有两个不同的功能(如上面的代码),按钮不起作用。

If you want to call function from url like codeigniter do, i have an example for you如果你想像 codeigniter 那样从 url 调用函数,我有一个例子给你

URL example: http://localhost/jastip/ajax/request.php/get_orders 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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM