After the document is ready, add the variable:
var fileCounter = 0;
Pass it to the first parameter of the append method, then increment it.
$.each($(".project_image")[j].files, function (i, file)
{
data.append(fileCounter, file);
fileCounter++;
});
, POST. , ajax POST Firebug. - , :
Content-Disposition: form-data; name="0";
EDIT: , a j append :
for(var j=0 ; j<= count ; j++)
{
$.each($(".project_image")[j].files, function (i, file)
{
data.append(j, file);
});
}
2
, multiple . , .
HTML:
<form name="ajax_image" id="ajax_image" method="post" enctype="multipart/form-data">
<input type="text" name="project_name" id="project_name" placeholder="Project Name" /><br/><br/>
<input type="text" name="project_duration" id="project_duration" placeholder="Project Duration" /><br/><br/>
<input type="file" name="project_images" class="project_images" placeholder="Project Image" multiple /><br/><br/>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
JS:
$(document).ready(function () {
$("#ajax_image").submit(function (event) {
var data = new FormData();
event.preventDefault();
$.each($(".project_images")[0].files, function (key, file){
data.append(key, file);
});
$.each($('#ajax_image').serializeArray(), function (i, obj) {
data.append(obj.name, obj.value)
});
$.ajax({
url: "upload.php",
type: "POST",
data: data,
processData: false,
contentType: false,
success: function () {
alert('Form Submitted!');
},
error: function () {
alert("error in ajax form submission");
}
});
});
});