0

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.

The Learner
  • 3,867
  • 14
  • 40
  • 50
  • 1
    see this http://stackoverflow.com/questions/950673/jquery-post-array – danqing Aug 05 '12 at 03:31
  • Are you trying to have the array sent to the server updated on the server side so that after the request is sent there will be new/different data than what was sent, or do you want the "xxxxx" to be your array? I'm slightly confused on what you want – Greg Rozmarynowycz Aug 05 '12 at 03:51
  • I am doing some computation in java script. I get the value from different source. It is a array with result.data[i].id and result.data[i].name. I want to send this array to my server using single post. – The Learner Aug 05 '12 at 03:58
  • Sorry if the edit is not what you're looking for, having trouble seeing exactly what you're trying to go for. – Greg Rozmarynowycz Aug 05 '12 at 04:23

3 Answers3

1

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);});
Marcelo Assis
  • 5,136
  • 3
  • 33
  • 54
  • serializeArray does not return an string according the documentation – Greg Rozmarynowycz Aug 05 '12 at 03:34
  • @GregRozmarynowycz, thanks! I was taking it was JSON.encode, but instead of JSON, in better to pass a straight string in this case. – Marcelo Assis Aug 05 '12 at 03:44
  • @marcelo Thanks for the replies...This is not working can you please correct me what I am missing. I am getting: %5Bobject+Object%5D%2C%5Bobject+Object%5D%2C%5Bobject+Object%5D... I have 2 arrays result.data[i].id and result.data[i].name. want to send one aray at a time – The Learner Aug 05 '12 at 03:59
  • This is what I tried: for(i=0;i – The Learner Aug 05 '12 at 04:01
0

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']

Greg Rozmarynowycz
  • 2,037
  • 17
  • 20
  • Greg, this resolved part of the issue I have.(the post I can see in firefox is having the array) but having issue in my php to access that data – The Learner Aug 05 '12 at 04:30
  • So as far as you can tell, the data being sent to the server is correct, but you can't access it in the PHP script? Try `var_dump($_POST)` and see what what the server returns. – Greg Rozmarynowycz Aug 05 '12 at 04:39
  • array(2) { ["ids"]=> array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" } ["names"]=> array(3) { [0]=> string(5) "Alice".. – The Learner Aug 05 '12 at 04:47
  • I can see that POST is going the way I want (thanks your suggestion,, I am not sending each post..) but having issues to access it – The Learner Aug 05 '12 at 04:48
  • So you mean that `$_POST['ids'][0] = null`? – Greg Rozmarynowycz Aug 05 '12 at 04:50
  • well I use: $IDs = $_REQUEST['ids']['0']; In that way it works. but I want to get the array and locally do the split ...that I am not getting – The Learner Aug 05 '12 at 04:54
  • If you want just the entire of array of IDs you would just have `$IDs = $_REQUEST['ids']`, without the zero. FYI, if you want to have code in your comment use back ticks (next to the '1' key on the keyboard). – Greg Rozmarynowycz Aug 05 '12 at 04:56
  • I realy did not get what you said.for how to use it as array. I tried $IDs = $_REQUEST['ids'] but did not work – The Learner Aug 05 '12 at 05:40
  • Can you add your PHP script, or at least the relevant parts of it, to your question? EDIT, and also just be sure, you don't want the answer the Bergi posted where you would have `$array['id'] = 'some name'`? – Greg Rozmarynowycz Aug 05 '12 at 05:45
  • I saw Bergi post but I did not try that..my main issue now is at the php side to access the values easily. with the suggestion you said:$IDs = $_REQUEST['ids']; var_dump($_POST); echo $IDs[0]; .. I have to do manually for the list of the array. so I am planning to send one more variable which has the i value and based on that I will try to get all the values in the aray. for(i<"received number>_ echo $IDS[i]; Thanks again for yourprompt responses. I will try some more things with this and will post if I have qs.. – The Learner Aug 05 '12 at 05:58
0

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375