-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?
– Andre Cardoso
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.
– Dunga Cardoso
@Adanribeiro, if you’re using
contentType
andprocessData
same as false, so instead of sending an Array, try sending an Formdata– Tobias Mesquita
@Dungacardoso this way as I am using it assigns to "arr" an input.all with all the parameters I need. See the print below.
– Adan Ribeiro
@Andrecardoso already yes, but for some reason I was not successful too.
– Adan Ribeiro
@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.
– Adan Ribeiro
Thank you @Tobymosque. Your suggestion was great.
– Adan Ribeiro