I'm trying to upload a picture with ajax.
This works if I do like this:
$("#adv_cover").change(function(e){
e.preventDefault();
var formData = new FormData($('#adv_form_cover')[0]);
$.ajax({
type: 'POST',
url: 'edit.php',
data: formData,
processData: false,
contentType: false,
success: function(response) {}
});
});
Then in PHP I get the data like this:
if($_FILES["adv_cover"]["name"]) {}
OK.
But now, I need to send an ID too, so this is how I do it according to some cases I found around here:
$("#adv_cover").change(function(e){
e.preventDefault();
var formData = new FormData();
formData.append('id', '1300');
formData.append('adv_cover', $('#adv_form_cover')[0]);
$.ajax({
type: 'POST',
url: 'edit.php',
data: formData,
processData: false,
contentType: false,
success: function(response) {}
});
});
Then in PHP I try to get the data like this:
$id=$_POST['id'];
if($_FILES["adv_cover"]["name"]) {}
But I don't get anything now.
I also tried to console.log both formData['id'] and formData['adv_cover'], with no success, so it seems that the problem is, at least, on the javascript side.
Note that I also tried both:
formData.append('adv_cover', $('#adv_form_cover')[0]);
and
formData.append('adv_cover[]', $('#adv_form_cover')[0]);
Thank you for your help