How to call a specific function in a PHP script via Ajax?

前端 未结 5 810
忘了有多久
忘了有多久 2021-02-06 15:53

Lets say I have a file called functions.php, and it has two separate functions inside:

One would get the time

And the other would get the date

How will I

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-06 16:20

    You could include a selector in the ajax request data. For example:

    $.ajax({
        url: "functions.php",
        data: "function=time", // or function=date if you want date
        ...
    });
    

    Then in your PHP code, a simple if-statement will check which one to output.

    if(isset($_GET['function'])) {
        if($_GET['function'] == 'time') {
            // do time stuff
        } elseif($_GET['function'] == 'date') {
            // do date stuff
        }
    }
    

提交回复
热议问题