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.

4
  • 1
    see this stackoverflow.com/questions/950673/jquery-post-array Commented Aug 5, 2012 at 3: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 Commented Aug 5, 2012 at 3: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. Commented Aug 5, 2012 at 3:58
  • Sorry if the edit is not what you're looking for, having trouble seeing exactly what you're trying to go for. Commented Aug 5, 2012 at 4:23

3 Answers 3

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);});
Sign up to request clarification or add additional context in comments.

4 Comments

serializeArray does not return an string according the documentation
@GregRozmarynowycz, thanks! I was taking it was JSON.encode, but instead of JSON, in better to pass a straight string in this case.
@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
This is what I tried: for(i=0;i<result.data.length;i++) { result.data[i].id; } var stringArray = result.data.id.join(","); $.post("receiver.php", { frdslist: stringArray }, function(data){ console.log(data);}); its not working
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']

10 Comments

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
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.
array(2) { ["ids"]=> array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" } ["names"]=> array(3) { [0]=> string(5) "Alice"..
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
So you mean that $_POST['ids'][0] = null?
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.