How do I get the files in AJAX ?
ThisistheAJAXthatisworking,butIamnotpassingthefilesasdate,IamgettinginPHP
$('#btnenviarpdf').click(function(){varid=$('#modalADDPDF').data('id');$.post('estrutura/upaddpdf.php',{acao:'inserir',id:id},function(r){varm=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 have done this, but the same problem continues.
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>