Ajax- submit form using ajax (With file/ image)

Below is the HTML and script to submit form using ajax (With file/ image)

HTML

1
2
3
4
5
<form action="#" name="uploader1" method="post" enctype="multipart/form-data" id="MyUploadForm">
    <span class="img1">Image 1</span><input name="file_upload" id="imageInput" type="file">
    <input type="submit" name="submit">
    <span id="success1"></span>
</form>

Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
jQuery("form[name='uploader1']").submit(function(e) {
        var formData = new FormData(jQuery(this)[0]);
        jQuery.ajax({
            url: "http://your-domain/processupload.php",
            type: "POST",
            data: formData,
            async: false,
            success: function (msg) {
               // alert(msg)
                jQuery( "#success1" ).html(msg);
            },
            cache: false,
            contentType: false,
            processData: false
        });
 
        e.preventDefault();
    });

Note: Please be sure, jQuery libary has included for the page.

No comments:

Post a Comment