天天看点

php 获取 xmlhttp.send,如何將XMLHttpRequest中的查詢字符串參數發送到php頁面

I have following php variable of name

我有以下php變量名

$name=$_POST["name"];

pge = '<?php echo $name ;?>';

var url = "searchCustomerByNameApi2.php"

//some code

xmlhttp.open("GET", url, true);

xmlhttp.send();

how can I send pge variable along with url as aquery string and access it on the php page ??

如何將pge變量連同url作為查詢字符串發送並在php頁面上訪問?

4 个解决方案

#1

1

Simple you have your javascript code look like this.

很簡單,javascript代碼是這樣的。

var name = '';

name = '<?php print $_POST["name"]?>';

var url = "searchCustomerByNameApi2.php?name"+name;

xhttp.open("GET", url, true);

xhttp.send();

and php code look like searchCustomerByNameApi2.php page

php代碼看起來像searchCustomerByNameApi2。php頁面

$name = $_REQUEST['name'];

#2

1

Try "searchCustomerByNameApi2.php?pge="+pge and get it on php page like $_REQUEST['pge'].

嘗試“searchCustomerByNameApi2.php嗎?pge="+pge,在php頁面上獲取,如$_REQUEST['pge']。

#3

You can use a function to create a query string from an object:

可以使用函數從對象創建查詢字符串:

And append that result to your url:

並將結果附加到你的url:

$name=$_POST["name"];

function encodeQueryData(data) {

let ret = [];

for (let d in data)

ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d]));

return ret.join('&');

}

pge = '<?php echo $name ;?>';

var data = {pge: pge};

var query =encodeQueryData(data);

var url = "searchCustomerByNameApi2.php";

url += "?" +query;

xmlhttp.open("GET", url, true);

xmlhttp.send();

#4

Have you tried below?

你試過下面嗎?

var url = "searchCustomerByNameApi2.php";

var test = "name=<?php echo $_POST["name"]; ?>";

xmlhttp.open("GET", url, true);

xmlhttp.send(test);

Hope this resolve your issue.

希望這能解決你的問題。