Im trying to upload a file using ajax posting to an object oriented class, but it isn't posting correctly.
Here is the ajax
var handleUpload = function(event)
{
event.preventDefault();
event.stopPropagation();
var fileInput = document.getElementById('file');
var data = new FormData();
for(var i = 0; i < fileInput.files.length; i++)
{
data.append('file[]', fileInput.files[i]);
}
var request = new XMLHttpRequest();
request.upload.addEventListener('progress', function(event)
{
});
request.upload.addEventListener('load', function(event)
{
});
request.upload.addEventListener('error', function(event)
{
});
request.open('POST', 'profile.php');
request.setRequestHeader('Cache-Control', 'no-cache');
request.send(data);
}
window.addEventListener('load', function(event)
{
var submit = document.getElementById('submit');
submit.addEventListener('click', handleUpload);
});
This is the php code that is calling the function in the class i created. The javascript successfully posts, however the $_FILES array isn't set so the php if statement isn't running.
if(!empty($_FILES['file']) && isset($_POST['special']))
{
$special = $_POST['special'];
$date = $_POST['date'];
$user->postSpecial($special, $date, $_FILES);
}