user2014429
user2014429

Reputation: 2577

show all file names from multiple file input in a single alert

I have this function which checks if selected file names are already in the database. And it alerts a message stating which files are in the database. The problem is it's a multiple file input, so if there is more than 1 file name which is in the database, it will make a separate alert for each file name. how can I change this so it makes just 1 alert which lists all the filenames(preferably with a space or comma after each one)? Thanks

 var file = $('#file')[0];
 $.get('all_filenames.php', function(data){
       for (var i=0; i<file.files.length; i++) {
       var fname = file.files[i].name;
       if(~data.indexOf(fname)){

          alert("these files already exist:" + fname);
       }
    }
 },'json');

Upvotes: 0

Views: 145

Answers (1)

howderek
howderek

Reputation: 2244

Try this, I pushed the filenames to an array.

JS

var file = $('#file')[0];
$.get('all_filenames.php', function (data) {
    var alreadyThere = [];
    for (var i = 0; i < file.files.length; i++) {
        var fname = file.files[i].name;
        if (~data.indexOf(fname)) {
            alreadyThere.push(fname);
        }
    }
    alert("These files already exist:" + alreadyThere.join(", ");
}, 'json');

Upvotes: 4

Related Questions