1

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;
}
Community
  • 1
  • 1
Alex
  • 1,982
  • 4
  • 37
  • 70
  • uploaded files do not appear in $_POST. They'd be in $_FILES only. – Marc B Apr 19 '16 at 14:46
  • Forgot to update the sample code, I first sent a `name` to the server, and only then checked for `file` (updated now) – Alex Apr 19 '16 at 14:48

0 Answers0