I have a form where I do a ajax image upload triggered on onChange event for the input element (file). The ajax image upload works fine but I notice that the image is uploaded again on submitting the form itself. How can I prevent the image from being uploaded again?
- make your button not to upload the file?bobek– bobek2011-11-18 18:21:01 +00:00Commented Nov 18, 2011 at 18:21
- Code, please! Is this something you downloaded or something you invented?Calvin Froedge– Calvin Froedge2011-11-18 18:23:48 +00:00Commented Nov 18, 2011 at 18:23
- I am using the code posted jquery4u.com/tutorials/thumbnail-image-upload-ajaxphparjunurs– arjunurs2011-11-18 18:26:40 +00:00Commented Nov 18, 2011 at 18:26
4 Answers
If the file input is being handled separately than the form (with an AJAX call) and shouldn't be part of the form, move it outside of the form tag. This will likely affect your page layout, so you may want to make some style changes. But think of it from the perspective of just looking at the HTML markup. Elements which aren't part of parent elements shouldn't be nested within those parent elements.
Comments
You can bind to the submit event for the form and clear the value of the file input so that it is blank when the form submits:
$('#your-form-id').on('submit', function () { $('#your-file-input').val(''); return true; }); You can also move the file input outside the rest of the form, so there will be two form tags but you can use position : absolute; to position the file input to appear as though it is inside the other form.