I want to send a 2D array to PHP via AJAX and then access the content in PHP. I have the following setup for creating the 2D array:
var data = [];
data['items1'] = ["item1_1", "item1_2"];
data['items2'] = ["item2_1", "item2_2"];
I then send this info to PHP via Ajax like this which works fine (I get to the case "my_php_case")
jQuery.ajax({
type: "POST",
url: ajax_site_url,
dataType: 'json',
data: {
action: "my_php_case",
data: data,
},
success: function(response) {
},
});
Now I want to access say "items1" on the PHP side and tried the following which gave an empty result:
$items1 = $_POST['data']['items1'];
I have tried JSON.stringify on data and then use json_decode on PHP side but still not working. I can only find more complex cases online when searching and just want this simple case to work.
How can I send and access the data with my Ajax call?
$_POSTvariable by usingvar_dump()orprint_r(). This docs about the.ajax()method (especially thedataoption description) may give you additional hints about why you have this: api.jquery.com/jquery.ajaxdata['items1']is not adding an item to an array, that is what you would do ifdatawas an object literal. To add to an array usedata.push(["item1_1", "item1_2"])