2

"company=1&cat=3&cat=1"

my Javascript object "cat" have two values 1,3 and

var cat = [];
cat.push(1);
cat.push(2);

now i want post it to servr using AJAX , I am able to post the data

and I can see data appearing in PHP side but my problem is

when I capture posted data in php it is only showing one value of cat

actual data = company=1 , cat= [1,3];

here is my posted query = company=1&cat=3

this is what PHP print_r showing =

 Array
(
    ["company] => 1
    [cat] => 3
    "
)

as you can see , the cat value is only one value instea of array (1,3)

what i am missing here ...

panindra
  • 646
  • 2
  • 11
  • 33

1 Answers1

3

You can use [] to pass an array. Your url should become like:

"company=1&cat[]=3&cat[]=1"

Then $_GET['cat'] will return array(3,1);

Daan
  • 12,099
  • 6
  • 34
  • 51
  • I just used $.param() to encode objects and it showing cat=1&cat=3 only . – panindra May 26 '15 at 11:11
  • What Daan is saying is, if you were to create html forms, with fields, you would need to set the names of these fields as "cat[]" in order for the posted result to be an array. – Relequestual May 26 '15 at 11:14
  • @relequestual I collection Options of Select from Jquery then i am pushing that to object 'cat' when I call console.log(cat) i can see result as ['1',3'] – panindra May 26 '15 at 11:16
  • OK, in which case, you need to re-name your from "cat" to "cat[]", using the name attribute. This is a related question with some code which you may find helpful if that doesn't solve the issue http://stackoverflow.com/questions/14766103/sending-html-form-multiple-select-box-via-post-request-with-ajax – Relequestual May 26 '15 at 12:17