I have a system that enables an upload of many images with XMLHttpRequest that work perfectly only that at the end it does not display a confirmation message even though i have place the confirmation message in the program.
Here is the Javascript :
<script>
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 xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress',function(event){
if(event.lengthComputable)
{
var percent = event.loaded / event.total;
var progress = document.getElementById('upload_progress');
while (progress.hasChildNones())
{
progress.removeChild(progress.firstChild);
}
progress.appendChild(document.createTextNode(Math.round(percent * 100) + '%'));
}
});
xhr.upload.addEventListener('load',function(event)
{
document.getElementById('upload_progress').style.display = 'none';
});
xhr.upload.addEventListener('error',function(event)
{
alert("failed");
});
xhr.open('POST','upload.php');
xhr.setRequestHeader('Cache-Control','no-cache');
xhr.send(data);
document.getElementById('upload_progress').style.display = 'block';
document.getElementById("upload_progress").innerHTML=xhr.responseText;
};
window.addEventListener('load',function(event){
var submit = document.getElementById('submit');
submit.addEventListener('click',handleUpload);
});
</script>
And the rest of the HTML
<div id="upload_progress"></div>
<form id="form" action="" method="post" enctype="multipart/form-data">
<input id="file" name="file[]" type="file" multiple /><br>
<input type="submit" name="send" id ="submit" value="send">
</form>
And finally my php code that enables the upload from upload.php
<?php
if(!empty($_FILES['file']))
{
###############################################################
foreach ($_FILES['file']['name'] as $key => $name)
{
move_uploaded_file($_FILES['file']['tmp_name'][$key],"myfiles/$name");
}
###############################################################
echo "God is good";
}
?>
Everything works perfectly, the files are uploaded However the confirmation message God is good does not display.
How to display a confirmation message with XMLHttpRequest after a multiple Upload ?