4
I’m trying to adapt an upload I’ve been using Ajax
, but I’m not getting it, so I read I need to use the formData
, I tried some alternatives, but all of them didn’t solve, my form has several fields and what I’m having trouble with is the upload.
What I have at the moment is this:
The form
<form enctype="multipart/form-data" class="form-horizontal" id="frmDoc" method="POST">
<div class="form-group">
<label class="col-md-2 control-label">Arquivo</label>
<div class="col-md-10">
<input type="file" class="btn btn-default" id="Arquivo" name="Arquivo">
<p class="help-block"> Extensão permitida <strong>PDF</strong>. </p>
</div>
</div>
Sending Ajax
:
$(document).ready(function(){ $(function () { // Validation $("#frmDoc").validate({ // Do not change code below errorPlacement: function (error, element) { error.insertAfter(element.parent()); }, submitHandler: function (form) { var data = $(form).serialize(); // console.log(data); $.ajax({ type: 'POST', url: 'pDocsNormativos.php', data: data, dataType: 'json', beforeSend: function () { $("#msgInsert").html('×AVISO! Enviando...'); }, success: function (response) { if (response.codigo == "1") { $("#msgInsert").html('×AVISO!' + response.mensagem + ''); } else { $("#msgInsert").html('×ATENÇÃO! ' + response.mensagem + ''); } // $('#frmDoc').each (function(){ // this.reset(); // }); }, error: function (xhr, ajaxOptions, thrownError) { console.log(xhr, ajaxOptions, thrownError); $("#msgInsert").html('×ATENÇÃO! Ocorreu um erro ao tentar enviar o Documento. Contate o suporte técnico.'); } }); return false; } }); }); });
I saw some examples but could not adapt, example as this link: Upload a file with AJAX
See if this helps: http://answall.com/questions/142834/enviando-uma-imagem-e-outros-dados-via-jquery-para-php
– Miguel
What a mistake you’re making?
– LocalHost
Hello @Localhost, I can’t send the file to upload, when I get to upload it is 'NULL'
– adventistapr
Is the data before sending going correctly? Like this your file that receives the data for upload?
– LocalHost
Most form fields are going correctly to the page . php which will upload the file, but the file will not.
– adventistapr