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

前端 未结 5 729
忘了有多久
忘了有多久 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 15:58

    What is the response your are getting. You are getting the response in XML or some other format. If your response is XML try with this option.

    $.ajax({
            url:path,           
                data:{projectId:inpprojectId},
                dataType:"xml",
                success:function(data){
                    $(data).find("CheckAmount").each(function(){
                        total = $(this).find("TotalAmount").text();
                        usdAmt = $(this).find("PettyCashAmount").text();    
                    validateBudget(total,inp);                  
                    });
                }       
        });
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-06 16:16

    You don't specify in jQuery what function to execute in PHP. What you do is request a document, possibly with a query string, and read the results. Your functions.php script is responsible for executing whatever is requested.

    So, you might from jQuery request functions.php?fn=time in one place and functions.php?fn=date in another. Then, in functions.php, you would examine the query string and execute the appropriate function for what was requested, returning the results to jQuery.

    0 讨论(0)
  • 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
        }
    }
    
    0 讨论(0)
  • 2021-02-06 16:20

    you can add a parameter to the url:

    example:

    function.php?g=1
    

    now, on the serverside check for the get parameter:

    if($_GET['g']==1)
    {
        echo date();
    }
    else
    {
        echo time();
    }
    
    0 讨论(0)
提交回复
热议问题