msm msm - 1 month ago 7
jQuery Question

jquery ajax call -submitting some form data

I have a webpage containing numerous controls.Some of them are 'file' controls which i use to upload images.
I use JQuery Ajax call to upload images.I have got the code to upload images from the internet itself long time back.The code is give below

$.ajax({
url: "upload.php",
type: "POST",
data: new FormData(this),<<------Submitting all the form data
mimeType: "multipart/form-data",
cache: false,
success: function(data){
$("#gallery").html(data);
},
error: function(){}
});


As you can see in the above code snippet, the entire form data are POSTED.Now suppose i don't post the entire data of the form but choose only to post data from the
say first file control 'file_1'.How should i go about it??What should i write above marked with an arrow.Hope you understand my question.

Answer

You can create a new FormData object and use its append() method to only add the data you require:

var fd = new FormData();
fd.append('foo', $('#bar').val());

$.ajax({
    url: 'upload.php',
    data: fd,
    // ...
});

Further reading on FormData available at MDN

Comments