Getting the files twice when submitting

Getting the files twice when submitting

I created a form that allows drag and drop for images. So what I've done is that when a user drops an image then the $("#fileuploader").on('drop', function(e) grabs the filename and then uses the $(this).trigger('got-dropped', [filename_list]); to then pass on the filename to the saveForm(). The problem I'm having with my code is that if I do submit a form with an image then my form is submitted twice, one with the image and one without the image. I know that the problem I'm having is due to this

  1. $('#fileuploader').on('got-dropped', function(event, files){
  2.     saveForm(files);
  3. });

  4. files = '';
  5. saveForm(files);
but I'm not sure on how I should go about finding out if there was a file uploaded or not. If I remove
  1. files = '';
  2. saveForm(files);

then I can't submit my form, because the saveForm() function only works if I've dropped an image, but if I keep the code then my form is submitted twice.

Here is my full js code
  1. $(document).ready(function()
  2. {
  3.     var getUrl = "http://cms.test";

  4.     $("#fileuploader").uploadFile({
  5.         url: getUrl+"/admin/includes/upload.php",
  6.         multiple: true,
  7.         dragDrop: true,
  8.         showPreview: true,
  9.         previewWidth: "100px",
  10.         showDelete: true,
  11.         fileName:"myfile"
  12.     });

  13.     $("#fileuploader").on('drop', function(e){
  14.         e.preventDefault();

  15.         var files_list = e.originalEvent.dataTransfer.files;

  16.         var filename_list = [];
  17.         for(x = 0; x < files_list.length; x++)
  18.         {
  19.             var filename = files_list[x].name;
  20.             filename_list.push(filename);
  21.         }

  22.         $(this).trigger('got-dropped', [filename_list]);
  23.     });

  24.     $('#fileuploader').on('got-dropped', function(event, files){
  25.         saveForm(files);
  26.     });

  27.     files = '';
  28.     saveForm(files);
  29. });

  30. function saveForm(files_list)
  31. {
  32.     var getUrl = "http://cms.test";

  33.     $(".create_menu").click(function(){
  34.         var menu_title = $('input#menu_title').val();
  35.         var type = $("input[name='type']:checked").val();
  36.         var main_menu_checkbox = $('input[name=main_menu]').is(':checked');
  37.         var side_menu_checkbox = $('input[name=side_menu]').is(':checked');

  38.         if(main_menu_checkbox == true)
  39.         {
  40.             var main_menu = 1;
  41.         }else{
  42.             var main_menu = 0;
  43.         }

  44.         if(side_menu_checkbox == true)
  45.         {
  46.             var side_menu = 1;
  47.         }else{
  48.             var side_menu = 0;
  49.         }

  50.         var files = files_list;

  51.         var selectParentMenu = $('#selectParentMenu').val();

  52.         var dataString = 'menu_title='+menu_title+'&type='+type+'&main_menu='+main_menu+'&side_menu='+side_menu+'&selectParentMenu='+selectParentMenu+'&files='+files;

  53.         $.ajax({
  54.             type: 'post',
  55.             url: getUrl+'/admin/menus.php?source=add_menu',
  56.             data: dataString,
  57.             success: function(){
  58.                 // $("#message").html("<h2>It has been submitted</h2>");
  59.             }
  60.         })
  61.     });
  62. }