繁体   English   中英

使用 AJAX (Javascript) 将查询传递到 Function

[英]Pass Query into Function using AJAX (Javascript)

我正在尝试使用 Javascript(不是 JQuery)ZA34A6659BCEAE7AB779F28185E 传递 PHP Function 参数我是否以某种方式将其添加到 javascript AJAX function 发送()? 我也将它发布到我的 PHP 文件中。

The main thing is how do I use the XMLhttp.send() function to asynchronously call a PHP function while add parameters to the function.

PHP 将以 POST 或 GET 的形式接收来自 JavaScript 的请求。 使用标准的$_POST$_GET超全局变量来访问这些值。

编辑 - 对不起,误解了这个问题

是的,使用 XMLHttpRequest 的send()方法。 前任:

var xhr = new XMLHttpRequest();
// ...
xhr.send("x=y&foo=bar");

然后,在 PHP 中:

echo( $_POST['foo'] ); // echos "bar"

无法从 Javascript 调用特定的 PHP function。 客户端对服务器端功能一无所知,服务器端对客户端一无所知。 jimbojw的回答解释了如何将参数传递到 PHP 并在 PHP 中处理它们。 If you want to call a specific PHP function and return the results to the client, then you're going to have to have code to figure out which function to call on the server, or make a separate php file for each function you want to称呼。

如果您想 go 第一条路线,那么您将传入类似“action”参数的内容,然后在包含您要调用的函数的 PHP 文件中执行此操作:

switch($_POST['action']) {
   case 'foo':
       foo($_POST['bar']);
       break;
   case 'foo2':
       foo2($_POST['bar']);
       break;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM