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
- $('#fileuploader').on('got-dropped', function(event, files){
- saveForm(files);
- });
- files = '';
- 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
- files = '';
- 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
- $(document).ready(function()
- {
- var getUrl = "http://cms.test";
- $("#fileuploader").uploadFile({
- url: getUrl+"/admin/includes/upload.php",
- multiple: true,
- dragDrop: true,
- showPreview: true,
- previewWidth: "100px",
- showDelete: true,
- fileName:"myfile"
- });
- $("#fileuploader").on('drop', function(e){
- e.preventDefault();
- var files_list = e.originalEvent.dataTransfer.files;
- var filename_list = [];
- for(x = 0; x < files_list.length; x++)
- {
- var filename = files_list[x].name;
- filename_list.push(filename);
- }
- $(this).trigger('got-dropped', [filename_list]);
- });
- $('#fileuploader').on('got-dropped', function(event, files){
- saveForm(files);
- });
- files = '';
- saveForm(files);
- });
- function saveForm(files_list)
- {
- var getUrl = "http://cms.test";
- $(".create_menu").click(function(){
- var menu_title = $('input#menu_title').val();
- var type = $("input[name='type']:checked").val();
- var main_menu_checkbox = $('input[name=main_menu]').is(':checked');
- var side_menu_checkbox = $('input[name=side_menu]').is(':checked');
- if(main_menu_checkbox == true)
- {
- var main_menu = 1;
- }else{
- var main_menu = 0;
- }
- if(side_menu_checkbox == true)
- {
- var side_menu = 1;
- }else{
- var side_menu = 0;
- }
- var files = files_list;
- var selectParentMenu = $('#selectParentMenu').val();
- var dataString = 'menu_title='+menu_title+'&type='+type+'&main_menu='+main_menu+'&side_menu='+side_menu+'&selectParentMenu='+selectParentMenu+'&files='+files;
- $.ajax({
- type: 'post',
- url: getUrl+'/admin/menus.php?source=add_menu',
- data: dataString,
- success: function(){
- // $("#message").html("<h2>It has been submitted</h2>");
- }
- })
- });
- }