0

In the Jquery code below, how do i send the "id" variable along with the "file" variable to process.php.

function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();

reader.onload = function(e) {
  $('#profile-img').attr('src', e.target.result);
}

var file = new FormData();
var id = 56;

file.append('file',input.files[0]);

$.ajax({
            url: "process.php",
            type: "POST",
            data: file,
            processData: false,
            contentType: false,             
            success:function(data){
            alert(data);
            }
        });

reader.readAsDataURL(input.files[0]);

} }

CoolDavies
  • 15
  • 8
  • The `FormData` interface has an [`append`](https://developer.mozilla.org/en-US/docs/Web/API/FormData/append) method which appears to do just what you want... – Heretic Monkey Jul 30 '18 at 21:57
  • Possible duplicate of [Sending file together with form data via ajax post](https://stackoverflow.com/questions/33761469/sending-file-together-with-form-data-via-ajax-post) – Heretic Monkey Jul 30 '18 at 21:58

1 Answers1

0

Do it via append of FormData just make sure you have input.files[0] set correctly

var formData = new FormData();
var id = 56;

formData.append('file', input.files[0]);
formData.append('id', id);

$.ajax({
   url: "process.php",
   type: "POST",
   data: formData,
   processData: false,
   contentType: false,             
   success:function(data){
       alert(data);
   }
});
Seva Kalashnikov
  • 4,262
  • 2
  • 21
  • 36