Reputation: 378
I use the below code to send data for image file with js and they works great but when I add
var image = document.getElementById("img_book").files[0];
var form = new FormData();
form.append("image", image);
I get error with append.
This my code
$("#publish").click(function() {
var x = event.target.responseText;
document.getElementById("book_id").setAttribute("value",x);
var image = document.getElementById("img_book").files[0];
var form = new FormData();
form.append("image", image);
var title = document.getElementById("title").value;
var desc = document.getElementById("desc").value;
var cat = document.getElementById("cat").value;
var sub_cat = document.getElementById("sub_cat").value;
var tags = document.getElementById("tags").value;
var lang = document.getElementById("lang").value;
var privacy = document.getElementById("privacy").value;
var id = document.getElementById("book_id").value;
//if((title !== "") && (desc !== "") && (cat !== "") && (sub_cat !== "") && (lang !== "") && (privacy !== "")) {
$.ajax({
url: "ajax/upload/publish.php",
method : "POST",
data : {'form' : form ,'title' : title,'desc' : desc, 'cat' : cat , 'sub_cat' : sub_cat , 'tags' : tags , 'lang' : lang , 'privacy' : privacy , 'id' : id},
cache: true,
success : function(data) {
if(data.status == 'success'){
window.location.replace("http://localhost/book/book.php?id=" + id);
}else if(data.status == 'error'){
alert("Error on query!");
}
}
});
//}
});
Upvotes: 0
Views: 64
Reputation: 97672
You have to append all the parameters to the formdata object, or more easily pass the form containing the data to the from data constructor.
Then pass the formdata object alone in the request, also contentType and processData has to be set to false.
Upvotes: 1