Tyler
Tyler

Reputation: 119

Sending multiple arrays with jQuery .post()

I have looked and can't quite come up with an answer to this question. I currently have an array which I call dataArray. The dataArray contains 2 keys (name, value) and is set up like this:
dataArray.push({name : file.name, value : this.result});
I am passing this array to my PHP file using the .post() method like so:
$.post('upload.php', dataArray[index], function(data) {}.
Then in the upload.php file I retrieve the data using:

$file = $_POST['value'];
$name = $_POST['name'];

This works fine for what I need. My question is how do I pass and retrieve 2 separate arrays. I have another array called 'position' that I would also like to send using the .post() method. This array is set up like so:
position.push({left : 0, top : 0});
I can't seem to find a way to pass them both at the same time.

Upvotes: 0

Views: 1770

Answers (2)

user796446
user796446

Reputation:

Can't you just do something like:

dataArray.push({left:0,top:0};

Based on the pattern you have shown you should then be able to retrieve the values with:

$left = $_POST['left'];
$right= $_POST['right'];

Upvotes: 0

Leonardo
Leonardo

Reputation: 736

You can always pass an javascript object with 2 arrays in it...

Like this:

arrayData= {
    array1 = [],
    array2 = []
};
$.ajax({
    type: "POST",
    url: 'http://post.url',
    data: arrayData
});

Retrieve in php script like this:

$arrayData = $_POST['arrayData'];
foreach($arrayData as $value) {
    //$value['array1']
    //$value['array2']
}

Upvotes: 2

Related Questions