Thursday, 15 April 2010

php - alert only file names selected from file input -


I have this Ajax call that checks if the name of multiple file input in a file already exists, and if When they do, a message warns them by telling them this. The problem is that I can only get it to alert all the file names from the result loop. How can I edit it so that it can only alert file names that were selected? Thank you.

  var file = $ ('# file') [0]; $ .get ('existing-filenames.php', function) {for (var i = 0; i & lt; file.files.length; i ++) {var fname = file.files [i] .name ;; (~ Data.indexOf (fname)) {// Only alert selected file name warnings ("These files already exist:" + data); return false;}}}, 'json');   

and existing-filenames.php

  $ allfiles = $ db- & gt; Query ("Select file from files where email = '$ _SESSION [email]'); $ Result = []; While ($ files = $ allfiles -> fetch_assoc ()) {$ result [] = $ files ['filename']; } Echo json_encode ($ result);   

and html

  & lt; Input name = "file []" type = "file" id = "file" multiple / & gt;    

In doubt, add more jQuery:

  $ ('# File'). ('Change', function () {var self = this; $ .get ('existing-filenames.php', function (data) {$ .each (self.files, function () (if (data.indexOf (this .name)! = -1) {Warning ("These files already exist:" + + this.name);}});});});    

No comments:

Post a Comment