簡體   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