使用 jQuery AJAX 调用 PHP 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4152431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call PHP Function using jQuery AJAX
提问by n00bi3
I've read all the topics about my question but cannot solve my problem. I want to get php function result using jQuery AJAX.
我已阅读有关我的问题的所有主题,但无法解决我的问题。我想使用 jQuery AJAX 获取 php 函数结果。
function.php
函数.php
function generateCode() {
//code here
}
function generateResult() {
//code here
}
How can I catch the function result using jQuery AJAX? I don't want to change function.php structure because it related to another pages.
如何使用 jQuery AJAX 捕获函数结果?我不想更改 function.php 结构,因为它与其他页面相关。
Refer to using jquery $.ajax to call a PHP function, no answer can solve my problem. Thank You
参考using jquery $.ajax to call a PHP function,没有答案可以解决我的问题。谢谢你
回答by deceze
You cannot call a function in a PHP file from Javascript, full stop. The client (here: Javascript) can only communicate with the server (here: PHP) through the HTTP protocol. The HTTP protocol has no notion of files, functions or programming languages. This means you're limited to transmitting information via the URL or HTTP headers. As such, you can never call a PHP function from Javascript, all you can do is request a URL which returns data.
您不能从 Javascript 中调用 PHP 文件中的函数,句号。客户端(此处:Javascript)只能通过 HTTP 协议与服务器(此处:PHP)进行通信。HTTP 协议没有文件、函数或编程语言的概念。这意味着您只能通过 URL 或 HTTP 标头传输信息。因此,您永远不能从 Javascript 调用 PHP 函数,您所能做的就是请求一个返回 data 的 URL。
So, by requesting a URL, say http://example.com/myscript.php
, the script myscript.php
gets invoked. This script now has to figure out what it should do and output some response. This script can actually call PHP functions, e.g.:
因此,通过请求一个 URL,比如说http://example.com/myscript.php
,脚本myscript.php
被调用。这个脚本现在必须弄清楚它应该做什么并输出一些响应。这个脚本实际上可以调用 PHP 函数,例如:
// myscript.php
include 'functions.php'
echo generateCode();
As such, whenever you request the URL http://exmaple.com/myscript.php
, you will get the output of the function generateCode()
. You may notice that this works the same as when you visit a URL with a web browser. That's because this is the only mechanism to run code through a web server using the HTTP protocol and hence the only way Javascript can "call PHP functions". Javascript has no secret backdoor to call PHP functions directly.
因此,每当您请求 URL 时http://exmaple.com/myscript.php
,您都会获得函数的输出generateCode()
。您可能会注意到,这与使用 Web 浏览器访问 URL 时的工作方式相同。这是因为这是使用 HTTP 协议通过 Web 服务器运行代码的唯一机制,因此也是 Javascript 可以“调用 PHP 函数”的唯一方式。Javascript 没有直接调用 PHP 函数的秘密后门。
Hope this helps.
希望这可以帮助。
回答by Naresh
What I do is in JavaScript I pass PHP function name which I want to call. Like..
我所做的是在 JavaScript 中传递我想调用的 PHP 函数名。喜欢..
function MyFunction() {
jQuery.ajax({
type: "GET",
url: "function.php",
data: "call=generateCode",
success: function(response){
//do something
});
}
Here in data field "call" variable have function name to call from "function.php" file.
在数据字段中,“调用”变量具有要从“function.php”文件调用的函数名称。
in "function.php" I place below code to call the function
在“function.php”中,我将代码放在下面来调用函数
if($_SERVER['REQUEST_METHOD']=="GET") {
$function = $_GET['call'];
if(function_exists($function)) {
call_user_func($function);
} else {
echo 'Function Not Exists!!';
}
}
回答by kst
JQuery
查询
$.ajax({
url: "script.php",
type: "POST",
data: "id=1",
success: function(msg){
alert(msg);
}
}
PHP
PHP
<?php
$id = $_POST['id'];
echo "The id is ".id;
?>
You'll get php function result in msg
from $.ajax success
state.
您msg
将从$.ajax success
状态中获得 php 函数结果。