Pass value $_FILES via Jquery

Asked

Viewed 608 times

0

I have a form from which an attached file is sent by e-mail. The problem is that when I upload the file, it doesn’t seem to pass the value of $_FILES via formulário/jquery. See below:

HTML

<form role="form" class="contact-form" id="contact-form" method="post">
     <div id="success"></div>
     <div class="form-group">
         <label for="career-name">Nome</label>
         <input type="text" name="Nome" class="form-control" id="nome">
     </div>
     <div class="form-group">
          <label for="career-name">E-mail</label>
          <input type="text" name="Email" class="form-control" id="email">
     </div>
     <div class="form-group">
          <label for="career-name">Vaga pretendida</label>
          <input type="text" name="Vaga" class="form-control" id="vaga">
     </div>
     <div class="form-group">
          <label for="career-resume">Currículo</label>
          <input type="file" name="Arquivo" class="form-control" id="curriculo">
     </div>
     <button type="submit" class="btn btn-primary" id="submit"><i class="icon-ok"></i> Enviar</button>
</form>

JQUERY

<script type="text/javascript">
      $('#submit').click(function() {
      $.post("includes/enviar-curriculo.php", $(".contact-form").serialize(), function(response) {
          $('#success').html(response);
        $('#nome').val('');
        $('#email').val('');
        $('#vaga').val('');
        $('#mensagem').val('');
      $('#curriculo').val('');
        });
      return false;
      });
</script>

PHP file

<?php
....
$curriculo = $_FILES["Arquivo"];
echo $curriculo['name'];
....
?>

It is possible to pass values $_FILES via Jquery?

1 answer

0


Yes. At the event submit of your form, through your ajax you will send the data as new FormData(this). Following example.

<form name="form1" id="send" enctype="multipart/form-data">
  <input type="file" name="nome" />
</form>

and in your ajax

$.ajax({
  url: 'file.php',
  type: 'POST',
  data: new FormData(this),

  success: function(){  }
});

In your PHP you will capture the value as follows

<?php 
  echo $_FILES["nome"]["name"];
?>
  • Hello Mauro. The problem is in the $_FILES upload field. The other fields are going correctly.

  • Edited. Check now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.