0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Paddy Hallihan
  • 1,624
  • 3
  • 27
  • 76
  • No need to stringify it as JSON the decode it again via PHP. You can send the array directly if you suffix the name of the property you append to `formdata` with `[]`. See https://stackoverflow.com/questions/16104078/appending-array-to-formdata-and-send-via-ajax/28434829#28434829 – Mitya Jul 08 '19 at 10:24
  • Make sure you have json enabled. Check it in phpinfo – Nirav Bhoi Jul 08 '19 at 10:25
  • @Utkanos tried changing my JS to this:`console.log(price_array);//price_array = JSON.stringify(price_array);var formData = new FormData();for(var i=0; i string 'undefined' (length=9)1 => string '' (length=0)` – Paddy Hallihan Jul 08 '19 at 10:29
  • You don't need the loop - just `formData.append('price_array[]', price_array);` – Mitya Jul 08 '19 at 10:31
  • @Utkanos that doesn't work either it just returns `array (size=1) 0 => string ',' (length=1)` – Paddy Hallihan Jul 08 '19 at 10:33
  • @NiravBhoi yeah JSON is enabled v1.2.1 – Paddy Hallihan Jul 08 '19 at 10:37

1 Answers1

0

The issue was stringifying a multidimensional array, changed this to an object and it worked as expected.

Instead of declaring it like:

var myVar = [];

I just changed it to:

var myVar = {};
Paddy Hallihan
  • 1,624
  • 3
  • 27
  • 76