Home > Net >  How do I send id in array in form append?
How do I send id in array in form append?

Time:09-29

I have this delete function that when user click it will push the id into delete array. User can delete multiple picture by click one by one icon trash. And to save thier commit changes about the delete item they have to click on the save button. But I have problem in sending the data in the form append.

enter image description here

output that I want to achieve is it will send the id the format like this

[64,65]

But currently it only send like this

64,65

enter image description here

my code


<a href="#javascript:;" onclick="deleteDoc('${e.doc.id}')"></a>

const deleteDoc = (id) =>{
    deleteDoc_temp.push(id);
}

const update = () => {

    let deleteDoc =  deleteDoc_temp; 

    let formData  = new FormData();
 
    formData.append('deleted_attachment', deleteDoc);
   
    for (var pair of formData.entries()) { console.log(pair[0]  ', '   pair[1]); }
}



How do I fix this problem to make sure that I can achieve output in append form like this ?

[64,65]

CodePudding user response:

Use the function JSON.stringify to the array. In your case :

Change

    formData.append('deleted_attachment', deleteDoc);

to

    formdata.append('deleted_attachment', JSON.stringify(deleteDoc));

or else you could just do

    deleteDoc.forEach((item) => formData.append("deleted_attachment[]", item));
  • Related