7

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.

5 Answers 5

10

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

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

1 Comment

This is pretty much what I was saying, but with sample code (which is worth a thousand words). +1.
1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.