0

I am using Axios for uploading images.

Here is my HTML:

<form action="" method="post"> 
    <input type="text" id="name">
    <input type="files" multiple id="file">
    <button type="submit" onclick="submit_form()">Upload</button>
</form>

Axios code:

function submit_form(e) {
   e.preventDefault();
   var form = new FormData();
   var name = document.getElementById("name").value;
   var image = document.getElementById("file");
   for (i = 0; i < image.files.length; i++) {                         
        let file = image.files[i];
       form.append("files[" + i + "]", file);
   }
   form.append("name", name);
   axios.post("ajax.php", form, {headers: {'content-type': 'multipart/form-data'}})
   .then((r) => {
      console.log(r);
    })
}

ajax.php:

$data = json_decode(file_get_contents("php://input"), true);
echo json_encode($data);

In the console it shows me null

If my PHP code is wrong, then can someone give a simple example?

KazikM
  • 1,257
  • 5
  • 21
  • 29
Anuj Kumar
  • 102
  • 7

0 Answers0