Save file to Mysql database using ajax and php function

Asked

Viewed 435 times

1

I need to save files that I can already select in a modal using a js function. Below follows 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 Alter

            <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

1 answer

1

I do not understand very well, you want to send 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 as soon as the form is submitted it calls this function that sends the data

Browser other questions tagged

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