HTML:
<form id="confirmresourceform" enctype="multipart/form-data" method="post" style="display: none;">
<input id="uploadlecture" name="fileToUpload1" type="file" value="Upload Resources"/>
</form>
Javascript/Jquery/Ajax:
$("#submitchanges").on("click",function(){
//Upload files
var formData = new FormData(document.getElementById("confirmresourceform"));
formData.append('lecture', $('#uploadlecture')[0].files[0]);
$.ajax({
type: 'POST',
url: 'upload.php',
contentType: false,
processData: false,
data: formData,
success: function (data) {
console.log(data);
}
});
});
PHP(upload.php):
echo $_FILES['lecture']['name'];
Ok so I'm attempting to upload a file using ajax(on the click of a div instead of using the submit form thing). I've gotten jQuery to append the file data to formdata with the key 'lecture'. Currently the upload.php script has one line to echo the file name. However, when the ajax runs and calls the upload.php, I get the following error:
Undefined index: lecture in C:\wamp\www\TheClass\lesson_creator_page\upload.php on line 4
I'm pretty sure I've defined 'lecture' in the formdata and passed the formdata to the php. Is there anything I'm doing wrong and is the line formData.append('lecture', $('#uploadlecture')[0].files[0]);
correct?