Save file to MySQL database using ajax and php function

1

I need to save files that I can already select in a modal using a js function. Below is the modal html

<div class="modal fade" id="modal_arquivo">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">Selecione o arquivo</h4>
        </div>
        <div class="modal-body">
            <div class="row">
                <div id="painel_enviar_arquivo" class="col-sm-10 col-md-offset-1">       
                    <form method="post" name="ajax_form" id="ajax_form">
                        <label>Arquivo</label>
                        <div class="input-group">
                            <label class="input-group-btn">
                                <span class="btn btn-primary">
                                    Selecione&hellip; <input type="file" id="file_arquivo" accept="file_extension|image/*" name="file_arquivo" style="display: none;" >
                                </span>
                            </label>
                            <input type="text" class="form-control" readonly>
                        </div>
                        <input id="acao" name="acao" value="salva_arquivo" type="hidden" />
                        <input id="ex_users_arquivo" name="ex_users_arquivo" type="hidden" />
                    </form>

                         Try selecting one or more files and watch the feedback                      < / span> >                                                                   "/ >                                                                                                      Cancel                     Change                 

            <div id="painel_btns_inserir">
                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancelar</button>
                <button type="button" class="btn btn-primary" id="btn_modal_salvar_arquivo">Salvar</button>
            </div>
        </div>
    </div>
    <!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->

                                                                ×                 Confirmation                                           

Are you sure you want to leave this page? All unsaved data will be lost

                                          Cancel                 Confirm                                              
asked by anonymous 06.12.2017 / 11:57

1 answer

1

I do not quite understand, do you want to submit the form via ajax?

To send via ajax is simple, you can use this code:

jQuery("#ajax_form").submit(function(){
  var dados = new FormData(this);
  var url = "URL QUE RECEBE OS DADOS DO FORM";
  $.ajax({
      url: url,
      type: 'POST',
      dataType: 'JSON',
      data:  dados,
      mimeType:"multipart/form-data",
      contentType: false,
      cache: false,
      processData:false,
      success: function(data)
      {
         alert("Enviado com sucesso");
      }
  });

  return false;
});

Ready with this code once the form is submitted it calls this function that sends the data

    
06.12.2017 / 12:22