0
I am in need of help with the following situation
I have several input files fields with the same name and which are used to do various file uploads.
<form>
<label> Foto 3*4</label>
<input type="file" name="file[]" data-name="foto34">
<input type="submit">
<br/>
<label> Foto 16*9</label>
<input type="file" name="file[]" data-name="foto169">
<input type="submit">
</form>
And I would like to take the date attribute so that I can identify which file name is being uploaded via ajax I’ve tried to take it with
this.getAtributte("name")
ou
this.dataset.name
in this last case returns me Undefined. The ajax call is being made like this:
$(document).ready(function(e){
$("#form").on('submit', function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'upload.php',
data: new FormData(this), //PRECISO PASSAR o DATA_ATRIBUTTE AQUI
contentType: false,
cache: false,
processData:false,
success: function(msg){
$(".btn").removeAttr("disabled");
}
});
});
});
Someone would know to help me?
$(document).ready(function(e){
$("#form").on('submit', function(e){
e.preventDefault();
var formulario = new FormData(this);
var files = $(":file");
console.log(files[0].value);
console.log(files[1].value);
// percorre os campos
files.each(function(i, e){
if($(e).val()){
formulario.append('data-name'+i, $(e).data("name"));
}else{
formulario.append(i, null);
}
});
$.ajax({
type: 'POST',
url: 'upload.php',
data: formulario,
contentType: false,
cache: false,
processData:false,
success: function(msg){
$(".btn").removeAttr("disabled");
}
});
});
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
input[type='file'] {
display: none
}
</style>
<form enctype="multipart/form-data" id="form">
<div class="col-md-4">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Foto 3x4</h3>
</div>
<div class="panel-body">
Panel content
<br />
<label for="file" class="bootstrap btn btn-info btn-xs" ><i class="fa fa-upload"></i> Selecionar Arquivo</label>
<input type="file" name="file[]" id="file" class="inputfile" data-name="foto34" >
<button type="submit" class="bootstrap btn btn-primary btn-xs"> Enviar</button>
</div>
</div>
</div>
<div class="col-md-4">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Foto 16x9</h3>
</div>
<div class="panel-body">
Panel content
<br />
<label for="file" class="bootstrap btn btn-info btn-xs"><i class="fa fa-upload"></i> Selecionar Arquivo</label>
<input type="file" name="file[]" id="file" class="inputfile" data-name="foto169">
<button type="submit" class="bootstrap btn btn-primary btn-xs"> Enviar</button>
</div>
</div>
</div>
</form>
if you can post the entire function from where you are trying to capture this information
– JrD
added to doubt
– tkmtts
Take the attribute date-name you say?
– LeAndrade
Exactly that
– tkmtts
Why you use multiple Submit buttons in the same form?
– Sam
The correct is
this.dataset.name
but it depends on where you’re calling it, because the value ofthis
can change, so it gives Undefined.– Sam
If you’re using the
this
within this Submit function will give Undefined even, because thethis
refers to the<form>
– JrD
@Sam for each input file, the client can upload. As the uploads are separated into bootstrap card, it was easier for the client to understand that at each upload he should submit the file, could upload via javascript, once loading the file, but could not do so
– tkmtts
Know.. so you want to send all the dates of all the file fields?
– Sam
@Sam yes, that’s even this being my difficulty, in sending everyone in an identified way
– tkmtts
Can’t you do that in PHP? For example, the first field file is the index [0], so in PHP you know that the image in index [0] is referring to "foto34", as well as the index [1] is "foto169".
– Sam
It could, but these upload inputs are mounted dynamically according to the user’s profile, so I don’t have a reference to the position of each, or even if it will exist. I could insert a logic for this, but I thought to do via Js
– tkmtts
Perhaps another way would be to make several formats for each document upload, but for me this would be very bad in practical terms
– tkmtts
@Sam any idea how you could help me?
– tkmtts
Already put a solution
– Sam