I want to know if is possible to call a PHP script passing params and uploading an image via POST at same time using AJAX
Imagine this form
Insert a title <input id="title" style="width:300px"></input>
Attach an image <input id="immagine" type="file" name="immy" accept="image/*" size="40"/>
The title is easly transmittable writing
var ajaxReq = new XMLHttpRequest();
var params = "titolo="+$('#title').val()
var url = "registraDati.php";
ajaxReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxReq.setRequestHeader("Content-length", params.length);
ajaxReq.setRequestHeader("Connection", "close");
ajaxReq.send(params)
While for the image is necessary use the FormData. Something like this (note that it don't works actually):
var file_data = $("#immagine").files[0]; //<----PROBLEM HERE!!
var form_data = new FormData();
form_data.append("afile", file_data)
form_data.append("user_id", 123)
var xhr = new XMLHttpRequest();
xhr.open('POST', 'uploadImmagine.php', true);
xhr.send(form_data);
Can i achieve both operations with one move?