I've developed a dynamic add/remove input type file to upload files, it is working well, I needed it to clone same ids and names and it is doing so, the problem is when I hit submit button, it stores only the first input's file and ignore the others, even shows only one file in console network tab's header in binary format, I tried searching but no luck, please guide me Thanks Note: Not using multiple attribute because I want one field to carry only one file at a time...
Here's the code for html:
<div class="row margin-bottom">
<div class="col-md-12 col-sm-12">
<button type="button" class="add-box btn btn-info"><span class="glyphicon glyphicon-plus"></span> Add More Fields</button>
</div>
</div>
<div class="form-horizontal">
<div class="text-box form-group">
<div class="col-sm-4"><input type="file" class="form-control" name="documents[]" id="documents"/></div>
</div>
</div>
here's the code for add/remove fields:
<script type="text/javascript">
$(document).ready(function(){
$('.add-box').click(function(){
var n = $('.text-box').length + 1;
if(n > 5) {
alert('Only 5 Savy :D');
return false;
}
var box_html = $('<div class="text-box form-group"><div class="col-sm-4"><input type="file" class="form-control" name="documents[]" id="documents" /></div><div class="col-sm-2"><button type="button" class="remove-box btn btn-danger btn-sm"><i class="fa fa-minus-circle fa-lg"></i></button></div></div>');
$('.text-box:last').after(box_html);
box_html.fadeIn('slow');
});
$('.form-horizontal').on('click', '.remove-box', function(){
$(this).closest(".form-group").remove();
return false;
});
});
</script>
here's the code for submit:
var getDocuments = document.getElementById('documents').files.length;
for (var x = 0; x < getDocuments; x++) {
formData.append("documents[]", document.getElementById('documents').files[x]);
}
idmust be unique. Use a class instead.