Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

ajax

php

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.

like image 532
sidewinder Avatar asked Jun 16 '11 12:06

sidewinder


1 Answers

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
    }
}
like image 72
hughes Avatar answered Nov 15 '22 01:11

hughes