I saw some simple code for using formdata for uploading images over ajax but on php side it is showing up NULL inside $_POST
, I tried to echo $_POST['file']['name']
but it echos nothing...
My php file has:
var_dump($_POST);
How can I use formData sent by ajax to php (in php)?
$(document).on('change','#image',function(){
var fd = new FormData();
fd.append( 'file', $(this).prop("files")[0]);
console.log(fd);
$.ajax({
type: 'post',
processData: false,
contentType: false,
url: './images.php',
data: fd,
success: function(a){
console.log(a);
}
});
});
#image{display:none}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<label for="image"><i class="fa fa-camera" aria-hidden="true"></i></label><input type="file" name="file" id="image">
</div>