0

jquery form submission prevention using e.preventDefault() is not working in multipart form submission ( when im using ajax to submit ). Is there any way to solve it ?

Code

$('.imgChange').submit( function(e){
    load('do_upload.php',$(this).serialize());
    e.preventDefault();
});
Arjun Raj
  • 984
  • 2
  • 12
  • 32
  • works for me http://jsfiddle.net/wM7rT/ – John Dvorak Nov 16 '12 at 08:57
  • possible duplicate of [jQuery - how to submit a \`post\` \`multipart/form-data\` from a form and get your function called?](http://stackoverflow.com/questions/7924157/jquery-how-to-submit-a-post-multipart-form-data-from-a-form-and-get-your-f) – Fardin K. Jul 13 '13 at 10:33

2 Answers2

1

This should work on multipart form also

HTML

<form id="form-id" action="/UploadFile" enctype="multipart/form-data" method="post">
    <input type="submit" value="Submit" />
</form>

jQuery

$('#form-id').submit( function(e){
    e.preventDefault();
    alert('submit prevented');
    // rest of the code here
});

As long as you do not have anything else overriding this like an onsubmit, there is no reason for .preventDefault() to not work on multipart forms

Here's a jsbin

Abhilash
  • 1,610
  • 9
  • 19
0

Using normal jQuery library it is not possible sent multipart form using ajax. There are many other plugin for that.

I recommend jQuery Form Plugin

it is very easy to use. Only you have to do is import that plugin after jQuery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
<script src="http://malsup.github.com/jquery.form.js"></script>

then use

$(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $('#myForm').ajaxForm(function() { 
            alert("Thank you for your comment!"); 
        }); 
    }); 
Arjun Raj
  • 984
  • 2
  • 12
  • 32