Not try and upload again when a different button is used to submit the form

Archived from the Transloadit support forum. Originally posted on February 25, 2014; details may be outdated, see our docs for current behavior. Original thread

Hi

I need to keep the file input tag inside a single form that is on my page. I cannot use more than one.

I’m using the jquery upload so that the image is uploaded when the file is selected. All working great.

But, when I press my other submit button, the system seem to try an upload an empty form …

Is it possible to have the transloadit not do ‘anything’ if a js variable is set to false somewhere? Or something similar / better?

Thanks,
Matt

Hey Matt,

could you please show me the code? Do you have this online somewhere?

You could unbind Transloadit from your form at run time using

$('#upload-form').unbind('submit.transloadit');

Kind regards,
Tim

Co-Founder Transloadit
@tim_kos

Hi Tim

Thanks for your response.

I have a solution working here: http://www.gm4sys.co.uk/mtemp/transup/issue.htm

Please:
- choose a file and watch it upload - don’t enter a server key value - try and submit the form

[this works nice as the loader is unbound]

But, if at that point, someone wants to change their file?

I tried rebinding or running init again, but it either didn’t load or loaded twice when I next tried to upload a replacement file before submitting.

What I really want is to be able to upload a file and get the url back without it being tied to my form submit …

However, love the results - so happy to use a workaround.

Thanks,
Matt

Hey Matt,

did what you said. During the first upload I simply hit “cancel” in the jQuery plugin’s modal and then uploaded a different file. It all seemed to work fine, which it should.

We have also set up our demo section to work like this https://transloadit.com/demos

Please let me know if you need further help.

However, love the results - so happy to use a workaround.

Great! :slight_smile:

Kind regards,
Tim

Co-Founder Transloadit
@tim_kos

Hm just saw a javascript error occur when I upload a file for the second time, so it’s still not working.

I see that you want form validation built in there. Have you seen our demo specific to this yet? https://transloadit.com/demos/misc/integrate-with-jquery-validate

Kind regards,
Tim

Co-Founder Transloadit
@tim_kos

Hi Tim

I just have that in there to show the need for JS calls to go in the on submit – my real page as many functions being called.

I just need the unbinding/re-binding to work or not be required without changing how my existing form works.

Would it be better if there was a dedicated upload button, rather than the automated method?

Thanks,

Matt

Hey Matt,

it would be easier to split this into two forms yes - one dedicated for uploading, one dedicated for your other fields. In the onSuccess callback of the jQuery plugin you can take the first parameter to it (the assembly object) and copy that to a new hidden form field in your second form and then set a flag that the second form can be submitted (or make its submit button visible then or whatever).

Kind regards,
Tim

Co-Founder Transloadit
@tim_kos

Hi Tim

Due to the layout of the page and how it is generated, I cannot spit the form into two forms.

  1. Is there an easy way to unbind when the main form submit button is pressed and re-bind is there is a form validation issue?
  2. Can I varify when transloadit is unbound and rebinded?
  3. Can I use a button to initiate an upload, rather than it just ‘happening’ when selecting a file?
  4. If autosubmit is set to false - when it it trying to upload when I press submit anyway?

Thanks,
Matt

Hey Matt,

  1. Yes.

Unbind: $(‘#upload-form’).unbind(‘submit.transloadit’);

Bind/rebind: $(‘#upload-form’).transloadit({ … });

  1. Yes.

var uploader = $(‘#upload-form’).data(‘transloadit.uploader’);
var isBound = !!uploader;

  1. Yes, just set triggerUploadOnFileSection to true in the options to the plugin.
  2. autoSubmit specifies whether to submit the original form automatically once the upload and processing have completed. So when you hit “Submit” the upload is then done of course. But once it is completed, it will not submit the original form if you set autoSubmit to false.

Please let me know if this helps.

Kind regards,
Tim

Co-Founder Transloadit
@tim_kos

Edit: Tender, our support system, ate my list numbers. -.-

Hi Tim

Thanks for the info. I think you’ve answered the opposite of my Q3.

With ref to my Q1. If the bind for the form is within another function, can I just call that?

Thanks,
Matt

Hey Matt,

not sure I fully understand. Can you please show a code sample of what you mean?

Hi Tim

The code remains the same:

www.gm4sys.co.uk/mtemp/transup/issue.cfm <http://www.gm4sys.co.uk/mtemp/transup/issue.cfm>

Can you make it so that the form is bound (at the start) and unbound (if a form validation error is found), then successfully re-bound after the message has been delivered? (without any js errors)

However, my question 3 was:

* Can I use a button to initiate an upload, rather than it just ‘happening’ when selecting a file?

o This meant, when you select a file – I want nothing to happen.

o I’d then have a button next to it called ‘upload file’, which when clicked would initiate the upload – rather than doing it automatically or if my main form is submitted.

The answer supplied was:

* Yes, just set triggerUploadOnFileSection to true in the options to the plugin.

Thanks again,

Matt

Hey Matt,

Can you make it so that the form is bound (at the start) and unbound (if a form validation error is found), then successfully re-bound after the message has been delivered? (without any js errors)

I don’t see any trouble why this shouldn’t be possible. Did you run into any issues?

I’d then have a button next to it called ‘upload file’, which when clicked would initiate the upload – rather than doing it automatically or if my main form is submitted.

Oh okay, that makes more sense now. So you basically want an optional file upload right? Like I said, this is best done in a separate form, where you would just attach the assembly JSON to your main form in the onSuccess(assembly) callback. If you do not use a separate, then the file will still be uploaded (to your server) in case someone selected a file for the file input field.

If you have more questions let’s move the discussion to Skype as that will be easier. My Skype username is transloadit_tim.

Kind regards,
Tim

Co-Founder Transloadit
@tim_kos

Hi Tim

On my demo page – when I try to unbind after a failed form validation.

The plugin still seems to stay bound:

http://www.gm4sys.co.uk/mtemp/transup/issue.htm

Am I doing something clearly silly?

Thanks,

Matt

Hi Tim

Even simpler:

http://www.gm4sys.co.uk/mtemp/transup/issue.htm

At the start it is bound.

Click unbind – the system seems to refuse to unbind it …

Thanks,

Matt

Hey Matt,

please note that unbinding it just unbinds the custom submit event on the form:

$(‘#frm_users’).unbind(‘submit.transloadit’);

… whereas you check for the presence of a dataset property:

var uploader = $(‘#frm_users’).data(‘transloadit.uploader’);
var isBound = !!uploader;

Both have nothing to do with each other. The proper check is something like:

var isBound = false;
$.each($(‘#frm_users’).data(‘events’), function(i, event){ if (event === ‘submitt.ransloadit’) { isBound = true; }
});

(untested)

We’ll add a “destroy” function to the jQuery plugin to make unbinding easier, so that your check will also work.

Kind regards,
Tim

Co-Founder Transloadit
@tim_kos

Hi,

I’m closing this ticket due to a long period of inactivity. But if you require more assistance please feel free to reopen!

Kind regards,
Kevin