I'm trying to send an object, with a file
as one of its properties using ajax
request to php
server (from there need to save it to mySql db). I read here, but still cannot make this work. I get the following error:
Uncaught TypeError: Illegal invocation
This is my JS:
var image;
$(':file').change(function(){
file = this.files[0];
image= file;
});
$('#submit').click(function(e){
var form_data = new FormData();
form_data.append('image', image);
var arr = {
name:$("#name").val(),
price:$("#price").val(),
discount:$("#discount").val(),
inventory:$("#inven").val(),
purchases:$("#purchase").val(),
category:$.trim($('.catButton').text()),
type:$.trim($('.typeButton').text()),
image:image
};
$.ajax({
url: 'server.php',
type: "POST",
data: form_data,
dataType: "JSON",
success: function (res) {
console.log(res);
},
error: function (erres) {
console.log(erres);
}
});
});
PHP:
if(isset$_FILES["image"])){
$name = $_FILES['image']['tmp_name'];
echo $name;
}