Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I am new in php and want to pass the array from javascript to php. On jquery side it should be like this:

var a= [];
a[0] = 'a';
a[1] = 'b';


$.ajax({
   type: "POST",
   data: {myarray:a},
   url: "index.php",
   success: function(msg){
     $('.answer').html(msg);
   }
});

which type on the server should I select?

share|improve this question

marked as duplicate by Shikiryu, gpojd, Donaudampfschifffreizeitfahrt, Andrew, Rubens May 25 '13 at 0:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
What exactly you mean by: Which type on the server should I select?? –  user1823761 May 24 '13 at 19:38
    
So, you want to get the a array in PHP? It's at $_POST['myarray']. –  Rocket Hazmat May 24 '13 at 19:38

3 Answers 3

In index.php you can get the data passed by the client side using $_POST['myarray']

$array = $_POST['myarray'];

$array[0] -> a

$array[1] -> b

Then do whatever you need to do and echo the response. This response will be your callback parameter msg in your $.ajax function

share|improve this answer

Use array type.

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);
share|improve this answer

If you are just looking to grab the POST values that are sent to php then this will echo out what the php is receiving from the request:

<?php
echo 'post values array items: ';
print_r($_POST);

$_POST is a super global which holds the POST data from the request that you are sending from your ajax request, more info on it can be found here:

http://php.net/manual/en/reserved.variables.post.php

share|improve this answer