1
I am developing an application in which, for aesthetic reasons I needed to recreate some elements to be able to properly customize each especially <select> and <option>.
So I need to pass the form capturing the date manually. For this, I am using:
  $('#criarConta').click(function(){
    $.ajax({
    url: "empregador.php",
    type: "POST",
    data: {
      nome:       $('#nomeSign').val(),
      email:      $('#emailSign').val(),
      senha:      $('#passSign').val(),
      local:      $('#localInput').val(),
      site:       $('#siteInput').val(),
      tipo:       $('#tipoPerfil').data('selected'),
      ramo:       $('ramoInput').data('selected'),
      profilepic: $('profilePicInput').val()
    },
    contentType: false,
    cache: false,
    processData:false,
    success: function(data){
      console.log(data);
    }
    });
  });
I put this profilepic there to see where I intend to call the image. As far as I know, the .val() will only recover the file path, not it itself.
How to solve my case?
You need to create a formdata to upload the image, take a look at this stack https://stackoverflow.com/questions/6974684/how-to-send-formdata-objects-with-ajax-requests-in-jquery
– Luiz Pillon
You want to send a server
arquivo/file?– Marconi
@Luizpillon And can I create a form data without the presence of a form? What would it look like in my example (manually adding)?
– Daniel Bonifácio
@Marconi I want to post the image along with the rest of the elements. I validate the image sent in my PHP later.
– Daniel Bonifácio
let form = new FormData(); form.append("campo", $('#input').val()); form.append("file", $("input2").prop('files')[0]);– Valdeir Psr
@Valdeirpsr put an answer :)
– Marconi
@Valdeirpsr worked perfectly! Plus let me validate the checkbox more easily! Thank you very much.
– Daniel Bonifácio