Hello, good afternoon everyone, I have a little problem when I try to send an image or file by ajax, what happens is that I am using a typeahead and tagmanager library to add tags with a query to the DB and I store it in an array to later process them where I did it well, but when add new FormData(this); to upload files in the post does not send me anything.
That using in my other forms without that of tags works fine but the array that is stored in js does not work with upload files at the same time array and upload file.
When I do it without a normal file, I process but with the file it gives me a problem, I hope your help.
Image 1 when it does not have the normal file, it is sent to the post.
Picture 2 when I add the file in the JS
JS
<script type="text/javascript">
var miArray = [];
v = 0;
function quitararea(v) {
valor = v - 1;
miArray.splice(valor, 1);
console.log(miArray);
}
$(document).ready(function() {
var tags = $(".tm-input").tagsManager();
jQuery(".typeahead").typeahead({
source: function(query, process) {
return $.get('consulta_sql.php', {
query: query
}, function(data) {
data = $.parseJSON(data);
return process(data);
});
},
afterSelect: function(item) {
var divisiones = item.split(" - ");
agregar = [divisiones[0], divisiones[1]];
for (var i = 0; i < miArray.length; i++) {
if (miArray[i][0] == divisiones[0]) {
alert("Ya se encuentra agregada la área!");
$('#area').val('');
return miArray;
break;
}
}
tags.tagsManager("pushTag", divisiones[1]);
$('#area').val('');
miArray.push(agregar);
console.log(miArray);
return miArray;
}
});
});
$("#enviar").submit(function(event) {
arraypasar = JSON.stringify(miArray);
var datos = $(this).serialize();
var arrayparametros = {arraypasar: arraypasar,datos: datos};
/* var data = new FormData(this); */
$.ajax({
type: 'post',
cache: false,
url: "post.php",
data: arrayparametros,
/* data: data,
contentType: false,
processData: false, */
success: function(data) {
$("#resultado").html(data);
}
});
event.preventDefault();
});
</script>
PHP
$data = json_decode($_REQUEST['arraypasar']);
/* $archivo = $_FILES["archivo_add"]['name']; */
parse_str($_REQUEST['datos'], $valores);
/* echo $archivo; */
echo "<br>";
echo $valores['area2'];
echo "<br><br>";
echo "<pre>";
print_r($data);
echo "</pre>";
Hello, well, I solved it in the following way. I had to separate the tags array from the submit by calling the function inside post.php to process the created array. I didn't want to separate it but that idea occurred to me, I was thinking of doing it in one.
Well there I leave my code as I solved it for those who use tags.
Greetings.