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?

3 Answers3

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

letiagoalves
  • 11,224
  • 4
  • 40
  • 66
0

Use array type.

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);
Sergey
  • 7,933
  • 16
  • 49
  • 77
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

tomo661
  • 88
  • 4