Skip to content Skip to sidebar Skip to footer

Send Image With Ajax To Database (php) Without Reloading

I create a bootstrap modal popup form to add picture in gallery page, I developed the php & ajax code because i want without page reloading here :- the problem is when I want

Solution 1:

You need to make few changes in your AJAX script, such as:

  • Set the following options, processData: false and contentType: false in your AJAX request. From the documentation,

    contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8') Type: Boolean or String When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header.

    and

    processData (default: true) Type: Boolean By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

  • And if you're uploading file through AJAX, use FormData object. But keep in mind that old browsers don't support FormData object, FormData support starts from the following desktop browsers versions: IE 10+, Firefox 4.0+, Chrome 7+, Safari 5+, Opera 12+.

So change your jQuery script in the following way,

$(function(){
    //twitter bootstrap script
    $("button#submit").click(function(){
        $.ajax({
            type: "POST",
            url: "image.php",
            data: newFormData($('form')[0]),
            processData: false,
            contentType: false,
            success: function(msg){
                $("#thanks").html(msg)
                $("#myModal").modal('hide');
            },
            error: function(){
                alert("failure");
            }
        });
    });
});

Post a Comment for "Send Image With Ajax To Database (php) Without Reloading"