How do you pass both text input as well as a file in one jQuery AJAX call? The code I have below passes the text input fields well but the file input returns an error (see below).
var formData = new FormData();
formData = {
'name' : $('input[name=name]').val(),
'email' : $('input[name=email]').val(),
'superheroAlias' : $('input[name=superheroAlias]').val()
};
formData.append('profile_pic', $('input[id="profile_pic"]')[0].files[0]);
// process the form
$.ajax({
type : 'POST',
url : 'process.php',
data : formData,
dataType : 'json',
processData : false,
contentType : false,
encode : true
})
On trying to receive the request on the PHP end like this:
$target_dir = "image_uploads/";
$target_file = $target_dir .rand(1,999). basename(str_replace(" ","",$_FILES["profile_pic"]["name"]));
I get the following error:
Undefined index: profile_pic
I have checked out a couple of solutions here on Stack Overflow already but none has done the trick for me yet.
How can I resolve this?