Dropzone Submit Button on Upload

Thought I'd add a pure vanilla JS solution as well, no jQuery.

/* 'dropform' is a camelized version of your dropzone form's ID */
      Dropzone.options.dropform = {
        /* Add all your configuration here */
        autoProcessQueue: false,

        init: function()
        {
          let myDropzone = this;
          /* 'submit-dropzone-btn' is the ID of the form submit button */
          document.getElementById('submit-dropzone-btn').addEventListener("click", function (e) {
              e.preventDefault();
              myDropzone.processQueue();
          });

          this.on('sending', function(file, xhr, formData) 
          {
            /* OPTION 1 (not recommended): Construct key/value pairs from inputs in the form to be sent off via new FormData
               'dropform' is the ID of your dropzone form
               This method still works, but it's submitting a new form instance.  */
              formData = new FormData(document.getElementById('dropform'));

             /* OPTION 2: Append inputs to FormData */
              formData.append("input-name", document.getElementById('input-id').value);
          });
        }
      };

NOTE: Setting up Event Listeners such as the sending one we are doing here should be put inside the init function. If you were to place them elsewhere, for example:

init: function() 
{
    //...
},
sending: function(file, xhr, formData) 
{
  //... logic before each file send
}

this will override the default logic dropzone provides for the sending Event listener, and can cause unintended side effects. You should only do this if you know what you're doing.


You need to:

  1. Add a button:

    <button type="submit" id="button" class="btn btn-primary">Submit</button>
    
  2. Tell Dropzone not to automatically upload the file when you drop it, as it will by default. That's done with the autoProcessQueue config option:

    autoProcessQueue: false
    
  3. Since Dropzone will now not auto-upload the files, you need to manually tell it to do that when you click your button. So you need an event handler for that button click, which tells Dropzone to do the upload:

    $("#button").click(function (e) {
        e.preventDefault();
        myDropzone.processQueue();
    });
    
  4. That will just POST the uploaded file, without any of your other input fields. You probably want to post all fields, eg your refCampaignID, a CSRF token if you have one, etc. To do that, you need to copy them into the POST before sending. Dropzone has a sending event which is called just before each file is sent, where you can add a callback:

    this.on('sending', function(file, xhr, formData) {
        // Append all form inputs to the formData Dropzone will POST
        var data = $('form').serializeArray();
        $.each(data, function(key, el) {
            formData.append(el.name, el.value);
        });
    });
    

Putting it all together:

Dropzone.options.frmTarget = {
    autoProcessQueue: false,
    url: 'upload_files.php',
    init: function () {

        var myDropzone = this;

        // Update selector to match your button
        $("#button").click(function (e) {
            e.preventDefault();
            myDropzone.processQueue();
        });

        this.on('sending', function(file, xhr, formData) {
            // Append all form inputs to the formData Dropzone will POST
            var data = $('#frmTarget').serializeArray();
            $.each(data, function(key, el) {
                formData.append(el.name, el.value);
            });
        });
    }
}