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"])) {

}
5
  • 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. Commented Mar 21, 2020 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. Commented Mar 21, 2020 at 10:05
  • Also, as items is an array, adding to fileData as fileData.append('items[]', items); should work? Commented Mar 21, 2020 at 10:06
  • Still [object Object] showing Commented Mar 21, 2020 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]. Commented Mar 21, 2020 at 10:20

1 Answer 1

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]));
}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
can u suggest something here? stackoverflow.com/questions/60785395/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.