1

I want ask about how to remove a values of files images before upload.The problem for me is when i delete one file image the input type file is not remove value and after that i press button send to php code it still show all files.

 window.onload = function(){
        
    //Check File API support
    if(window.File && window.FileList && window.FileReader)
    {
        var filesInput = document.getElementById("files");
        
        filesInput.addEventListener("change", function(event){
            
            var files = event.target.files; //FileList object
            var output = document.getElementById("result");
            
            for(var i = 0; i< files.length; i++)
            {
                var file = files[i];
                
                //Only pics
                if(!file.type.match('image'))
                  continue;
                
                var picReader = new FileReader();
                
                picReader.addEventListener("load",function(event){
                    
                    var picFile = event.target;
                    
                    var div = document.createElement("div");
                    
                    div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
                            "title='" + picFile.name + "'/> <a href='#' class='remove_pict'>X</a>";
                    
                    output.insertBefore(div,null);  
                    div.children[1].addEventListener("click", function(event){
                       div.parentNode.removeChild(div);

                    });         
                
                });
                
                 //Read the image
                picReader.readAsDataURL(file);
            }                               
           
        });
    }
    else
    {
        console.log("Your browser does not support File API");
    }
}
 body{
    font-family: 'Segoe UI';
    font-size: 12pt;
}

h1{
    font-size:12pt;
    color: #fff;
    background-color: #1BA1E2;
    padding: 20px;

}
article
{
    width: 80%;
    margin:auto;
    margin-top:10px;
}


.thumbnail{

    height: 100px;
    margin: 10px;    
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>More file uploads</title>


</head>

<body>
   <h1>Select multiple files</h1>
    <article>
 <form action="#" method="post" enctype="multipart/form-data">
        <input id="files" type="file" name="files[]" multiple/>
        <output id="result" />
  <input type="submit" name="btnsubmit" value="Send"/>
 </form>
 </article>
 
  <?php 
    if(isset($_POST['btnsubmit'])){
   var_dump($_FILES['files']['name']);
 }
  ?>
</body>
</html>
Andy
  • 41
  • 6

1 Answers1

0

I had seen your code, I think the code blow is just remove the div, but not change the $_FILES

div.children[1].addEventListener("click", function(event){
    div.parentNode.removeChild(div);
 });
gangzi
  • 105
  • 1
  • 13