sidewinder
sidewinder

Reputation: 3163

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

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, using JQuery AJAX retrieve data from the function that retrieves the date. How do I specify in the JQuery code which function on the server to pick.

I hope I am making sense. Thanks.

Upvotes: 7

Views: 22197

Answers (5)

simbu94
simbu94

Reputation: 1012

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);                  
                });
            }       
    });

Upvotes: 1

Ben
Ben

Reputation: 1535

$(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.

Upvotes: 6

hughes
hughes

Reputation: 5713

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
    }
}

Upvotes: 10

sorpigal
sorpigal

Reputation: 26116

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.

Upvotes: 7

Sujit Agarwal
Sujit Agarwal

Reputation: 12518

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();
}

Upvotes: 1

Related Questions