你如何通过GET将javascript数组传递给php?

时间:2012-10-22 14:19:03

标签: php javascript

我必须通过http GET将amountname数组从javascript传递到php脚本。

var amount=new Array();   
amount = document.form_perk.elements["amount[]"];  
var name = document.form_perk.elements["name[]"];
http.open('get', 'perks.php?+obj2url("amount", amount)&obj2url("name", name)');   
http.onreadystatechange = insertReply;  
http.send(null);

这是怎么做到的?

1 个答案:

答案 0 :(得分:0)

编辑:看来问题是:

http.open('get', 'perks.php?+obj2url("amount", amount)&obj2url("name", name)'); 

应该是:

http.open('get', 'perks.php?' + obj2url("amount", amount) + '&' + obj2url("name", name)');

原始邮件:

使用此功能(从here修改):

function EncodeQueryData(arr_data, str_prepend) {
   str_prepend = str_prepend || "";
   var ret = [];
   for (var d in arr_data)
       ret.push(encodeURIComponent(str_prepend + d) + "=" + encodeURIComponent(arr_data[d]));
   return ret.join("&");
}

var base_url = "perks.php";
var query_string = EncodeQueryData(amount, "amount") + '&' + EncodeQueryData(name, "name");
window.location = base_url + '?' + query_string;

这应该给你举例如下:

perks.php?amount0=10&amount1=27&name0=john&name1=sally