0

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?

3
  • make your button not to upload the file? Commented Nov 18, 2011 at 18:21
  • Code, please! Is this something you downloaded or something you invented? Commented Nov 18, 2011 at 18:23
  • I am using the code posted jquery4u.com/tutorials/thumbnail-image-upload-ajaxphp Commented Nov 18, 2011 at 18:26

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Would it work if I wrap the file input tag in a form and nest it within the other form? I guess I will have to try that
Since I don't know the structure of your HTML I made a simple example of my second suggestion: jsfiddle.net/c3M6W
0

I think you have to submit the form using javascript in onchange event

Comments

0

In your HTML, form action should be empty:

<form action=""> 

In JS code use:

$('#submut').click(function(){ var //get var by jQuery }); //Your $ajax code here... 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.