1
How do I get the files on AJAX?
That’s the AJAX that is working, however I am not passing as date the files, I’m taking the PHP
$('#btnenviarpdf').click(function () {
var id = $('#modalADDPDF').data('id');
$.post('estrutura/upaddpdf.php',{acao:'inserir',id:id},function(r) {
var m = jQuery.parseJSON(r);
if (m.success) {
$('#modalADDPDF').modal('hide');
toastr["success"](m.msg);
$("#listapdf").load(location.href + " #listapdf>*", "");
} else {
toastr["error"](m.msg);
$('#modalADDPDF').modal('hide');
}
});
});
This error occurs:
Notice: Undefined index: img in upaddpdf.php on line 14
PHP:
<?php
include("../includes/config.php");
if($_POST){
$data = array('success' => '0',
'msg' => 'Ocorreu um erro, nada foi inserido!');
$acao = $_POST['acao'];
$id = $_POST['id'];
IF($acao != ""){
//INFO IMAGEM
$file = $_FILES['img']; <<<=== linha 14
$numFile = count(array_filter($file['name']));
//PASTA
$folder = 'arquivos/anexos';
Now I’ve done it this way, but it’s still the same problem.
JQUERY
$('#btnenviarpdf').click(function () {
var id = $('#modalADDPDF').data('id');
var form_data = new FormData();
var ins = document.getElementById('multiFiles').files.length;
for (var x = 0; x < ins; x++) {
form_data.append("files[]", document.getElementById('multiFiles').files[x]);
}
$.post('estrutura/upaddpdf.php',{acao:'inserir',id:id},function(r) {
var m = jQuery.parseJSON(r);
if (m.success) {
$('#modalADDPDF').modal('hide');
toastr["success"](m.msg);
$("#listapdf").load(location.href + " #listapdf>*", "");
} else {
toastr["error"](m.msg);
$('#modalADDPDF').modal('hide');
}
});
});
PHP
<?php
include("../includes/config.php");
if($_POST){
$data = array('success' => '0',
'msg' => 'Ocorreu um erro, nada foi inserido!');
$acao = $_POST['acao'];
$id = $_POST['id'];
IF($acao != ""){
//INFO IMAGEM
$file = $_FILES['files'];
$numFile = count(array_filter($file['name']));
Form:
<form id="formulario" method="post" enctype="multipart/form-data" action="">
<input type="hidden" name="acaopdf" value="">
<input type="hidden" name="id" value="<?php echo $id?>">
<div class="file-field">
<div class="d-flex justify-content-center">
<div class="btn btn-info btn-rounded btn-sm">
<span id="spa">Escolha o arquivo</span>
<input type="file" id="mulitplefileuploader" name="img[]" multiple>
</div>
</div>
<center><input class="btn btn-info btn-rounded btn-sm" id="btnenviarpdf" type="submit" value="Enviar" /></center>
</div>
</form>
Have you tried to var_dump or print_r the $_FILE variable, what was the return? Or try to inspect the request and see if the files are being sent.
– Bilico
I already did that, day the variable was not set..
– Jefferson Meireles
I do the editing because it misreads the existing response and does not give details of what the error was, please do as I said, tell me what error occurred using the browser console and look in the network tab (if your browser is in English the tab name is "Network"), if you do not know how to do this view this answer https://answall.com/a/62797/3635
– Guilherme Nascimento
thanks for the help, but nothing happens, can be my form
– Jefferson Meireles