0

We can create a javascript array like bellow

    var array = { name:"jhon", desg:"manager" };

We can send the object directly as data with an $ajax request, and all value can be accessed using $_POST array. Ex : $_POST['name'] will return 'jhon'.

Now I have a for loop where I want to make the array dynamic. Something like :

    for(var i=0; i<8; i++){
        array.push(a+id:"me"+id);
    }

Now I will send array in $ajax request and in request handler I will be able to retrieve data in $_POST array. Ex : $_POST['a0'] will return 'me0'.

I tried many way but failed, do you have any way to solve this.

2 Answers2

0

You should use this:

array[a+id] = "me"+id;

(Note that a better name would be "object" rather than an "array".)

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
0

First of all you should clear your mind, that is an object not an array.

Try something like bellow. I am using variable data instead of array.

var data = {}; //creating a blank object

for(var i=0; i<8; i++){
    data['a'+id] = me+'id';
}

In your PHP you will get content in $_POST variable, so $_POST['a0'] will return you 'me0'

Mritunjay
  • 25,338
  • 7
  • 55
  • 68