Having some trouble with my file upload using AJAX to post to an object-oriented PHP class.
Here's the AJAX code:
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);
});
Below is the PHP code calling a function in my created class. The JavaScript posts successfully, but the $_FILES array isn't set, so the PHP if statement doesn't run.
if(!empty($_FILES['file']) && isset($_POST['special'])) {
$special = $_POST['special'];
$date = $_POST['date'];
$user->postSpecial($special, $date, $_FILES);
}