I'm trying to submit a form with AJAX, containing two variables and an input type="file" multiple.
Form works as it should if it's submited in the clasic way.
But when I try to AJAX it, the $_FILES[] array comes out empty on the .php side.
I've tried everything I know, doesn't work.
Here's the code:
HTML
<form action="ajax/process.php" enctype="multipart/form-data" method="post" id="admUploadImg">
<input type="hidden" name="postOp" value="adm-upload-img">
<input type="hidden" name="pid" value="<?= $prodData['id'] ?>">
<div class="row my-2 mx-0 justify-content-center">
<div class="col-12 col-sm-2 tb-1 text-left text-sm-right">Imagini
<label for="admProdImgs" class="btn btn-success btn-sm p-0 alert-success tip mb-1" title="Adauga imagini"><span class="icon-plus"></span></label>
<input type="file" multiple id="admProdImgs" name="productImgs[]" class="filestyle invisible" data-form="admUploadImg" required accept="image/jpeg,image/png">
</div>
</div>
Javascript
$('#admUploadImg').submit(function(e){
e.preventDefault();
var formData = new FormData(this);
$.post('ajax/process.php', formData, function(response){
console.log(response);
});
console.log(formData);
});
However, this generates the following error:
TypeError: 'append' called on an object that does not implement interface FormData.
So, I've changed the AJAX to that:
$('#admUploadImg').submit(function(e){
e.preventDefault();
var formData = $('#admUploadImg').serialize();
$.post('ajax/process.php', formData, function(response){
console.log(response);
});
console.log(formData);
});
But this time, the $_FILES[] array comes empty.
It's driving me crazy, I have no idea why it doesn't work. Help!