1

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?

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

3 Answers 3

1

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

0

Use array type.

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);
0

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