I have a form with different sections, when a user saves a section it is sent via JS to another file where it saves it as a session variable in PHP until the rest of the form is filled out and then it is all processed together.
Here is my JS that sends my array:
console.log(price_array);
price_array = JSON.stringify(price_array);
var formData = new FormData();
formData.append("price_array", price_array);
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
//session set
}
};
xmlhttp.open("POST", "myFile.php", true);
xmlhttp.send(formData);
So at this point I can see my array exactly the way I want when I log it in the console before I stringify it and send it.
(2) [empty, Array(0)]1: Array(0)EUR: Array(0)cost: "100"discount: "10"margin: "10"z1: "0"z2: "10"z3: "20"z4: "30"z5: "40"z6: "50"z7: ""z8: ""length: 0__proto__: Array(0)GBP: Array(0)cost: "100"discount: "10"margin: "10"z1: "0"z2: "10"z3: "20"z4: "30"z5: "40"z6: "50"z7: ""z8: ""length: 0__proto__: Array(0)attribute: "default"model_no: "test"length: 0__proto__: Array(0)length: 2__proto__: Array(0)
My PHP file then is very simple it is just:
if(isset($_POST['price_array'])){
$_SESSION['price_array'] = json_decode($_POST['price_array']);
echo 'session_set';
exit();
}
However, when I do a var_dump($_SESSION['price_array']);
it appears blank. Can anyone explain why this is happening or what I can do about it?
array (size=2)
0 => null
1 =>
array (size=0)
empty
Edit
I did this:
console.log(price_array);
var json = JSON.stringify(price_array);
console.log(json);
And got this:
(2) [empty, Array(0)]
1: Array(0)
EUR: [cost: "1830.00", discount: "31.00", margin: "28.00", z1: "0.00", z2: "110.00", …]
GBP: [cost: "1525.00", discount: "31.00", margin: "28.00", z1: "50.00", z2: "0.00", …]
attribute: "1-1_"
model_no: "FMS 20M"
length: 0
__proto__: Array(0)
length: 2
__proto__: Array(0)
[null,[]]
So when it is being stringified something is going wrong is this because it is a multidimensional array?