如何调用通过Ajax PHP脚本的特定功能? [英] How to call a specific function in a PHP script via Ajax?

查看:132
本文介绍了如何调用通过Ajax PHP脚本的特定功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一个名为functions.php文件,它里面有两个单独的函数:

Lets say I have a file called functions.php, and it has two separate functions inside:

一会获取时间

和其他会得到的日期

我将如何,使用JQuery AJAX检索,检索日期功能的数据。我如何在JQuery的code服务器上的哪些功能来接指定。

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.

推荐答案

您可以包括Ajax请求数据的选择。例如:

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

然后在你的PHP code,一个简单的if语句将其中一个检查输出。

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

这篇关于如何调用通过Ajax PHP脚本的特定功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆