The Learner
The Learner

Reputation: 3917

Jquery post a Array

I am having a Array which is generated by my Javascript in run time. once that array is full with all the values I want to send it using POST to the server. How can I do that ...

Pseudo code:

 for(i=0;i<result.data.length;i++)
         {
           result.data[i].id

    }   
   $.post("receiver.php", { xxxxx }, function(data){ console.log(data);});

How can I get that xxxx updated in the post

I checked the documentation in jquery but they are expecting to give all the values in POST.I do not want to do that. Also, I want to send post only once so that traffic will be less.

Upvotes: 0

Views: 298

Answers (3)

Bergi
Bergi

Reputation: 664217

Not sure, but it seems like you want to do this:

var sendObj = {};
for (var i=0; i<result.data.length; i++) {
    var id = result.data[i].id,
        name = result.data[i].name; // or some similiar computation
    sendObj[name] = id;
}   
$.post("receiver.php", sendObj, function(data){ console.log(data);});

This will send the result.data as name=id-value-pairs.

Upvotes: 0

Greg Rozmarynowycz
Greg Rozmarynowycz

Reputation: 2085

similar to iBlue's comment, You can just send an object with post; you don't have to define the object in the post function, { } are simply the delimiters to define objects, which are similar to PHP associative arrays:

$.post('reciever.php', myData, function(data){ /*callback*/ });

The only thing is that you setup myData as an object like follows:

myData = {
     0: 'info',
     1: 'info'
}

//or even something like this

myData = {
     someProp: 'info',
     someProp2: {
         anotherProp: 'moreInfo'
     }
 }

you can also use non-numerical indexes with objects, and easily add properties:

myData[2] = 'info';

or you can loop through it, just in a slightly different way:

for(i in myData){
     myData[i]; //Do something with myArr[i]
}

the for in loop will also loop through non-numerical properties. And you can still get the length of myData by

myData.length;

EDIT:

Instead of sending a string:

IDs = {}
Names = {}

for(var i = 0; i < result.data.length; i++){
    IDs[i] = result.data[i].id;
    Names[i] = result.data[i].name;
}
$.post('reciever.php', {IDs: IDs, Names: Names}, function(data){});

In the PHP file you would access them like so

$_POST['IDs'][0] = "some id";
$_POST['Names'][0] = "some name";

EDIT:

Actaully I think the indexes are sent as strings, so might have to do

$_POST['IDs']['0']

Upvotes: 0

Marcelo Assis
Marcelo Assis

Reputation: 5194

EDIT

You can use join() to get all your array values converted to a string, using some char to separate it.

EDIT 2: As Kumar said he was using 2 arrays

var idsArray;
var namesArray;
for(i=0;i<result.data.length;i++)
    {
        idsArray[] = result.data[i].id; 
        namesArray[] = result.data[i].name; 
    }  

var ids   = idsArray.join(",");
var names = namesArray.join(",");
$.post("receiver.php", { ids:ids, names:names }, function(data){ console.log(data);});

Upvotes: 1

Related Questions