0

About the issue

I am posting three things

  1. plain text
  2. Array of some plain text
  3. Image

While reading the posted data in php, I was only able to read [object, object] for the array data.

Below is the code in JQuery

Preparing the data to post to a php file.

var items = [];

$(Items).each(function(index, row) {
    var name = $(row).find("[name^='name']").val(); 
    var age = $(row).find("[name^='age']").val();           
    var sub1 = $(row).find("[name^='sub1']").val();         
    var sub2 = $(row).find("[name^='sub2']").val();         

    items.push({
        "name": name,
        "age": age,
        "sub1": sub1,
        "sub2", sub2
    });
});

var fileData = new FormData();

This is good

fileData.append('cust_first_name', $("[name='cust_first_name']").val());

This is the point of concern

fileData.append('items', items);

This is good

fileData.append('image1', $("[name='image1']").prop('files')[0]);

$.ajax({
    url: "myurl.php",
    cache: false,
    contentType: false,
    processData: false,
    data: fileData,
    type: 'post',
    success: function (response) {

    }
});

Below is the PHP code.

//$data = json_decode(file_get_contents('php://input'), true);

This is good

$cust_first_name = $_POST["cust_first_name"];

This is the point of concern as it shows [object, object]

$items = $_POST["items"];

foreach($items as $item) {
    echo "<pre>";
    print_r($item);
    echo "</pre>";  
}

This is good

if(isset($_FILES["image1"])) {

}
Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • **Side note:** Don't use `$data = json_decode(file_get_contents('php://input'), true);` as it receives anything it gets with any kind of request, not really respecting only the POST verb. Correct me if I am wrong. – nice_dev Mar 21 '20 at 10:04
  • That is a dead code. I forgot to remove it. As you can see that it is not being used in php code anywhere. – Pankaj Mar 21 '20 at 10:05
  • Also, as `items` is an array, adding to fileData as `fileData.append('items[]', items);` should work? – nice_dev Mar 21 '20 at 10:06
  • Still `[object Object]` showing – Pankaj Mar 21 '20 at 10:08
  • I might be wrong, but I don't think you can append an object to formData. The issue is that JS tries to convert it into a string, which results in `[object Object]`. – M. Eriksson Mar 21 '20 at 10:20

1 Answers1

2

To pass javascript objects to the backend, you will have to JSON.stringify them to pass them over the network(like serialization).

So change fileData.append('items', items); to

for(var i=0;i<items.length;++i){
   fileData.append('items[]', JSON.stringify(items[i]));
}
nice_dev
  • 17,053
  • 2
  • 21
  • 35
  • This time a string is showing like this in php side: `{"name": "edde", "age": 12, "sub1": "eded"}`. Can you suggest how to convert to an object? – Pankaj Mar 21 '20 at 10:25
  • can u suggest something here? https://stackoverflow.com/questions/60785395/jquery-validation-not-working-for-second-row-in-table – Pankaj Mar 21 '20 at 10:43