Suppose I have the following data:
var arr = [], arr1 = [], arr2 = [], arr3 = [], arr4 = []; var a = 'something', b = 'else'; arr1['key1-1'] = 'value1-2'; arr1['key1-2'] = 'value1-2'; for (var i = 0; i < someCond; i++) { arr = []; arr['key2-1'] = 'value2-1'; arr['key2-2'] = 'value2-2'; arr2.push(arr); }
Now I need to pass the hole to the php script.
I packed it into one variable, for example:
var postVar = { a: a, b: b, arr1: arr1, arr2: arr2 };
I am using jQuery, so I tried to publish it like this:
1)
//Works fine for a and b, not for the arrays $.post('ajax.php', postVar, function(response){});
and this:
2)
var postVar = JSON.stringify(postVar); $.post('ajax.php', {json: postVar}, function(response){});
with php file
$req = json_decode(stripslashes($_POST['json']), true);
which also does not work.
How do I structure / format my data to send it to PHP?
thanks
EDIT:
Case 1: console.log (postVar); 
PHP answer print_r ($ _ POST): array ([a] => something [b] => else)
As you can see, on the php side there are no arrays (objects).
Case 2:
When I add the following:
postVar = JSON.stringify (postVar);
console.log (postVar);
I get
{"a": "something", "b": "else", "arr1": [], "arr2": [[], [], []]}
with console.log (postVar)
So this is the problem in this case ... isn't it?