dsg7513
2018-12-04 15:35
浏览 204

如何通过jquery AJAX调用特定的php函数

I have a jquery ajax call to a php file. However, the file contains multiple defined functions, all somewhat similar.

$conn = mysqli_connect($servername, $username, $password, $dbname);


function loadall(){
    $querystring = "SELECT location, filetype, author FROM globalposts ORDER 
BY date DESC";
    $results = mysqli_query($conn, $querystring);

    $row = mysqli_fetch_array($results, MYSQLI_ASSOC);
    echo json_encode($row);
}

function jasperbriggsload(){
    $querystring = "SELECT title, location FROM omar ORDER BY date DESC";
    $results = mysqli_query($conn, $querystring);

    $row = mysqli_fetch_array($results, MYSQLI_ASSOC);
    echo json_encode($row);
}

I have not done overloaded methods with variables, though they might work with my db structure.

My main question is how to call a specific function in an external php through ajax. if I wanted to call loadall() or jasperbriggsload() through just ajax arguments

图片转代码服务由CSDN问答提供 功能建议

我有一个jquery ajax调用php文件。 但是,该文件包含多个已定义的函数,都有些类似。

  $ conn = mysqli_connect($ servername,$ username,$ password,$ dbname); 
 
 \  nfunction loadall(){
 $ querystring =“SELECT location,filetype,author FROM globalposts ORDER 
BY date DESC”; 
 $ results = mysqli_query($ conn,$ querystring); 
 
 $ row = mysqli_fetch_array($ 结果,MYSQLI_ASSOC); 
 echo json_encode($ row); 
} 
 
function jasperbriggsload(){
 $ querystring =“SELECT title,location FROM omar ORDER BY date DESC”; 
 $ results = mysqli_query(  $ conn,$ querystring); 
 
 $ row = mysqli_fetch_array($ results,MYSQLI_ASSOC); 
 echo json_encode($ row); 
} 
   
 
 

我没有使用变量重载方法,尽管它们可能适用于我的数据库结构。

我的主要问题是如何通过ajax在外部php中调用特定函数。 如果我想通过ajax参数调用 loadall() or jasperbriggsload() 通过

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douqian2524 2018-12-04 16:26
    已采纳

    You can use call_user_func passing the function name sent by ajax as argument. Like this:

    <?php
    function test(){
        //your code
    }
    
    //This will run the test() function
    call_user_func('test');
    
    //This will run the function named as the content of $_REQUEST['funcName']
    call_user_func($_REQUEST['funcName']);
    ?>
    
    已采纳该答案
    打赏 评论