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

前端 未结 5 737
忘了有多久
忘了有多久 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:00

    $(document).ready(function()
    {
        $(".link").click(function()
        {
            var data['func'] = "time";
            var url  = functions.php
            $.get(url, data, function(result)
            {
                $("#feedback").html(result);
            });
        });
    });
    

    then your php file would be,

    if(isset($_GET['func']))
    {
        if($_GET['func'] == "time")
        {
            showTime();
        }
        else
        {
            showDate();
        }
    }
    
    function showTime()
    {
        //show time
    }
    
    function showDate()
    {
        //show date
    }
    

    Code is untested but should be a good starting point for you.

提交回复
热议问题