2
I need to pass two more variables through ajax, I tried to do it this way:
JS:
var usuario_cod = document.getElementById("usuario_cod");
var line_cod = document.getElementById("line_cod");
...
$.ajax({
url: "envio_anexo_processo.php",
type: "POST",
data: {'usuario_cod':usuario_cod,'line_cod':line_cod,formdata,},
...
HTML:
<fieldset>
<div id="container" style="margin-top:50px;">
<div class="col-sm-offset-2 col-sm-8">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Enviar arquivos em PDF</h3>
</div>
<div class="panel-body">
<form method="post" enctype="multipart/form-data" action="/process_files.php">
<input type="file" name="files[]" id="input_files" multiple />
<input type="hidden" id="usuario_cod" name="usuario_cod" value="<?php echo mb_strtoupper ($usuario_cod);?>">
<input type="hidden" id="line_cod" name="line_cod" value="<?php echo mb_strtoupper ($line_cod);?>">
<button type="submit" id="btn_submit" class="form-control">Enviar Arquivos!</button>
</form>
<br />
<div id="loading_spinner"><i class="fa fa-spinner fa-pulse"></i> Enviando</div>
<div id="result"></div>
</div>
</div>
</div>
</div>
</fieldset>
<script type="text/javascript">
function TransferCompleteCallback(content){
// we might want to use the transferred content directly
// for example to render an uploaded image
}
$( document ).ready(function(){
var input = document.getElementById("input_files");
var formdata = false;
if(window.FormData){
formdata = new FormData();
$("#btn_submit").hide();
$("#loading_spinner").hide();
}
$('#input_files').on('change',function(event){
var i = 0, len = this.files.length, img, reader, file;
//console.log('Number of files to upload: '+len);
$('#result').html('');
$('#input_files').prop('disabled',true);
$("#loading_spinner").show();
for( ; i < len; i++ ){
file = this.files[i];
//console.log(file);
if(!!file.name.match(/.*\.pdf$/)){
if ( window.FileReader ){
reader = new FileReader();
reader.onloadend = function(e){
TransferCompleteCallback(e.target.result);
};
reader.readAsDataURL(file);
}
if (formdata){
formdata.append("files[]", file);
}
} else{
$("#loading_spinner").hide();
$('#input_files').val('').prop('disabled',false);
alert(file.name+' Não é um PDF');
}
}
var usuario_cod = $('#usuario_cod').val();
if (formdata){
$.ajax({
url: "envio_anexo_processo.php",
type: "POST",
data: formdata,
processData: false,
contentType: false, // this is important!!!
success: function (res){
//alert(data);
var result = JSON.parse(res);
$("#loading_spinner").hide();
$('#input_files').val('').prop('disabled',false);
if(result.res === true){
var buf = '<ul class="list-group">';
for(var x=0; x<result.data.length; x++){
buf+='<li class="list-group-item">'+result.data[x]+'</li>';
}
buf += '</ul>';
$('#result').html('<strong>Arquivos enviados:</strong>'+buf);
} else{
$('#result').html(result.data);
}
// reset formdata
formdata = false;
formdata = new FormData();
}
});
}
return false;
});
});
</script>
It didn’t work, as I do to send two variables?
If you are a beginner, it would be a good alternative to see a library called "Axios" https://github.com/axios/axios : )
– fullstack-overkill