Post via Ajax arrives empty to PHP

Asked

Viewed 555 times

-1

I have some values selected by checkboxes that I would like to send to be processed in another PHP script.

When I send the post via ajax to a page that receives the $_POST variable and prints it, I just view "Array (0){}".

var objeto = {flag: "exportar"};
var arr = [];
var seletores = document.querySelectorAll(".meusCheckBoxes");
for(i = 0; i < seletores.length; i++){
  if(seletores[i].checked){
    arr.push(seletores[i]); 
  }
}
objeto['dados'] = arr;
$.ajax({
  url: "Url",
  data: objeto,
  type: "POST",
  contentType: false,
  processData: false,
  success: function(retorno){
    $(".conteudo").html(retorno); //array(0) { } 
    console.log(objeto); //Object {flag: "exportar", dados: Array[5]}
  }
}); 

I think the problem is to send an Object with an array inside, because when sending separately I can see the return.

I need to send an Object because the index ("export", in this case) has to be associative, something that the javascript array does not have.

Note: In "Success" the console prints the Object with the array correctly posted.

  • Already tried to send a JSON instead of the array?

  • Should arr.push(selectors[i]) not be something like arr.push(selectors[i].value) or checked?.. because I have the impression that only selectors[i] will not send any information.

  • @Adanribeiro, if you’re using contentType and processData same as false, so instead of sending an Array, try sending an Formdata

  • @Dungacardoso this way as I am using it assigns to "arr" an input.all with all the parameters I need. See the print below. imagem ilustrativa

  • @Andrecardoso already yes, but for some reason I was not successful too.

  • @Tobymosque I didn’t use Formdata in this example simply because the layout I was looking for the information didn’t "make it easy" to use this tool. But it’s something I can try. I’ll post here the result.

  • 1

    Thank you @Tobymosque. Your suggestion was great.

Show 2 more comments

2 answers

1

After the comment of @Tobymosque suggesting the use of Formdata, in addition to adding a form in HTML I made the following modifications to my JS:

/*var objeto = {flag: "exportar"};
var arr = [];
var seletores = document.querySelectorAll(".all");
for(i = 0; i < seletores.length; i++){
  if(seletores[i].checked){
    arr.push(seletores[i]); 
  }
}
objeto['dados'] = arr;*/

var objeto = new FormData(document.querySelector(".meuForm"));
objeto.append("flag", "exportar");

$.ajax({
  url: "URL",
  data: objeto,
  type: "POST",
  contentType: false,
  processData: false,
  success: function(retorno){
    $(".conteudo").html(retorno);
    console.log(objeto);
  }
}); 

And it was totally OK!

The initial problem was not exactly solved, but this solution proved to be better than my initial one. Thank you all!

0

PHP only takes the value of the object, in case you are sending an object to php, where it will not recognize the object you are receiving. Utilize:

arr.push(seletores[i].value);

And send with ajax with JSON, as it is easier to work with it.

Browser other questions tagged

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