Gillean_KoSU
Gillean_KoSU

Reputation: 3

Empty values sending FILE using FormData, AJAX and PHP

When I send an AJAX request using FormData I don't get values ​​in either $ _POST or $ _FILES and I can't understand why is this happening.

This is the following form:

<form method="post" id="form1" enctype="multipart/form-data">                       
   <input type="file" id="img" name="first_imp">                              
</form>

This is the Jquery code:

var formData = new FormData(document.getElementById("form1"));                   
$.ajax({
        type: 'POST',
        url:'/mod_tanque',
        processData: false,
        contentType: false,
        data: {
            data:formData,
        },success (response) {
            console.log(response);
        }
 });

And PHP file:

var_dump($_FILES);
var_dump($_POST);

When I show the content of $_FILE or $_POST, this is the result:

array(0) {}
array(0) {}

Upvotes: 0

Views: 57

Answers (1)

charlietfl
charlietfl

Reputation: 171679

The only thing you want to pass to the ajax data is the FormData object

Change

$.ajax({
     // ...
     data: {
        data:formData,
     },

To

$.ajax({
     // ...
     data: formData,

Upvotes: 3

Related Questions