Send several js files

0

I have a form inside a modal and I send the data by post using ajax.

The problem is that you can send several files at once but I get the following error:

jquery-1.11.1.min.js:4 Uncaught RangeError: Maximum call stack size exceeded
at Vc (jquery-1.11.1.min.js:4)

.html file

<div class="modal fade" id="modalFac" tabindex="-1" role="dialog" aria-
 labelledby="exampleModalLabel" aria-hidden="true">
   <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
       <h5 class="modal-title" id="exampleModalLabel">Formulario para enviar 
   Factura</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    <form class="form-horizontal" role="form" enctype="multipart/form-data">
      <div class="form-group">
        <label for="apyn" class="col-sm-3 control-label">Nombre:</label>
          <div class="col-sm-9">
            <input type="text" class="form-control" id="nameFac" value="<?php echo $proveedor->nombre; ?>">
          </div>
      </div>

      <div class="form-group">
        <label for="cuit" class="col-sm-3 control-label">CUIT:</label>
          <div class="col-sm-9">  
            <input type="text" class="form-control" id="cuitFac" value="<?php echo $proveedor->cuil; ?>">
          </div>
      </div>

      <div class="form-group">
        <label for="correo" class="col-sm-3 control-label">Email:</label>
          <div class="col-sm-9">
            <input type="text" class="form-control" id="correoFac" value="<?php echo $proveedor->email; ?>">
          </div>
      </div>

      <div class="form-group">
        <label for="upload_file" class="col-sm-3 control-label" accept=".pdf">Factura/s: </label>
          <div class="col-sm-9">
            <input type="file" class="form-control" id="facturaFac" multiple>
          </div>
      </div>

      <div id="messageOKFac" class="alert alert-success hidden" style="text-align: center">
        La consulta se ha enviado con &#233;xito.
      </div>
    </form>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
    <button type="button" class="btn btn-primary" onclick="sendFacturas($('#formFac').serialize())">Enviar</button>
  </div>
</div>

.js

function sendFacturas(){
 var fd = new FormData();
 var files = document.getElementById('facturaFac').files[0];
 fd.append('file', files);
 var files1 = document.getElementById('facturaFac').files[1];
 fd.append('file1', files1);
 var files2 = document.getElementById('facturaFac').files[2];
 fd.append('file2', files2);
 var name = $("#nameFac").val();
 fd.append('name', name);
 var cuit = $("#cuitFac").val();
 fd.append('cuit', cuit);
 var email = $("#correoFac").val();
 fd.append('email', email);
$.ajax({
   url: 'mailFacturas.php',
   data: fd,
   type: 'POST',
   contentType: false, // NEEDED, DON'T OMIT THIS (requires jQuery 1.6+)
    processData: false, // NEEDED, DON'T OMIT THIS
    success: function (response) {
    if ($.trim(response) == 'OK'){
      $("#messageOKFac").removeClass("hidden");
      $('#nameFac').val("");
      $('#cuitFac').val("");
      $("#correoFac").val("");
      $('#facturaFac').val("");
    } else alert("FAIL");
    },
    error: function () {
     alert("error");
    }
});

And in mailFacturas.php I receive the variable and the attachment. I use phpmailer.

$nombre = $_POST['name'];
$cuit     = $_POST['cuit'];
$email    = $_POST['email'];
$mail = new phpmailer (); 
....
$mail->AddAttachment($_FILES['file']['tmp_name']);

Thanks

    
asked by GAL 15.06.2017 в 18:12
source

1 answer

2

Let's see, you must add each of the multiple input files to the formData.

var formData = new FormData();

var num= document.getElementById('facturaFac').files.length;

for (var i = 0; i < num; i++) {
    formData.append("facturaFac[]", document.getElementById('facturaFac').files[i]);
}

And in the PHP part you should attach to the mail each of the files that you have added. Right now you're just attaching one.

Change:

$mail->AddAttachment($_FILES['facturaFac']['tmp_name']);

By:

foreach( $_FILES['facturaFac']['tmp_name'] as $nombre_fichero) {
    $mail->AddAttachment($nombre_fichero);
}
    
answered by 16.06.2017 / 10:41
source