-3

I have to pass amount and name arrays from javascript to a php script via http GET.

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);

How is this done?

psubsee2003
  • 8,563
  • 8
  • 61
  • 79
shilpa
  • 17
  • 2
  • 9
  • read more about http://en.wikipedia.org/wiki/AJAX maybe that could help you – jpganz18 Oct 22 '12 at 14:33
  • @MrCode: JSON.stringify(amount) could produce the query part if you also replaced `:` and `,` with `=` and `&`. – Stefan Oct 22 '12 at 14:40
  • Or you could pass the entire JSON string (Ex. `str_json`) as a single query parameter and then do `$arr = json_decode($_GET['str_json'], true);` in the php. – Stefan Oct 22 '12 at 15:01

1 Answers1

0

EDIT: It appears the problem is this:

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

Should be:

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

ORIGINAL POST:

Use this function (modified from 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;

This should give you for example something like this:

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

Community
  • 1
  • 1
Stefan
  • 3,850
  • 2
  • 26
  • 39
  • i got perks.php?amount0=[object HTMLInputElement]&amount1=[object HTMLInputElement]&amountlength=2&amountitem=function item() {%0A [native code]%0A}&name0=[object HTMLInputElement]&name1=[object HTMLInputElement]&namelength=2&nameitem=function item() {%0A [native code]%0A} – shilpa Oct 23 '12 at 07:12
  • Use for loop first to create your arrays, or use jQuery to get your arrays: `var amount = []; $('input[name="amount[]"]').each(function(){ values.push($(this).val()); });` – Stefan Oct 23 '12 at 07:46
  • Or: `arr_amount = []; for (var i=0; i< amount.length; i++) { arr_amount[i] = amount[i].value; }`. Then pass `arr_amount` instead of `amount` to EncodeQueryData etc. – Stefan Oct 23 '12 at 07:47
  • @shilpa: Actually, it looks like you simply have a small mistake: Change `http.open('get', 'perks.php?+obj2url("amount", amount)&obj2url("name", name)');` to this: `http.open('get', 'perks.php?' + obj2url("amount", amount) + '&' + obj2url("name", name)');` – Stefan Oct 23 '12 at 08:09
  • how to get the first element of the array ...it gives value undefined – shilpa Oct 26 '12 at 07:36
  • The first element is `amount[0]`. You mean `amount = document.form_perk.elements["amount[]"];` gives `undefined`? Make sure that your HTML form inputs are named like this: `name='amount[]'`, i.e that there are brackets. – Stefan Oct 26 '12 at 08:09
  • I have done that..but when i entered only 1 value to amount and name field then while redirecting it gives perks.php?&&amount-length=undefined&name-length=undefined..even if its the first element I should be able to save it .how to do? – shilpa Oct 26 '12 at 08:48
  • Are you using `obj2url()` or are you using the `EncodeQueryData()` function? – Stefan Oct 26 '12 at 08:57
  • Try this: `amount = [].concat(amount);` and `name = [].concat(name);` before using them in `obj2url()` or `EncodeQueryData()` – Stefan Oct 26 '12 at 09:01
  • Thanks for your reply..It worked.I am using EncodeQueryData().As in obj2url() while inspecting in FF(DOM) i get responseText as Array ( [obj2url("amount",_amount)___] => [obj2url("name",_name)__] => [obj2url("pdescrb",_pdescrb)__] => [obj2url("number",_number)__] => [obj2url("estimatedate",_estimatedate)] => ) – shilpa Oct 26 '12 at 09:24