0
I’ve been struggling with a problem for days..
Here’s the thing, I have a multidimensional array and I want to pass as a parameter in a request ajax for a php file , however when I’m doing this what is returning me is a php empty. I have no idea why , I researched and found nothing that corresponded to my doubt.
If anyone can give me a light!
Follow the code!
// Criando o array global
var dados = new Array();
dados[contador] = new Array();
dados[contador]['usuarios'];
dados[contador]['quantidade'];
var usuario = false;
//Verificando se usuario já existe
for (var i = 0; i < dados.length; i++) {
if (dados[i]['usuarios'] === usuarioDivisao) {
dados[i]['usuarios'] = usuarioDivisao;
dados[i]['quantidade'] = $("input[name='mailing_quantididade_html']").val();
usuario = true;
break;
}
}
if (!usuario) {
dados[contador] = new Array();
dados[contador]['quantidade'] = $("input[name='mailing_quantididade_html']").val();
dados[contador]['usuarios'] = usuarioDivisao;
contador++;
}
$("input[name='mailing_quantididade_html']").val("");
After populating them I am passing as parameter to the php file
$('.atualizar').click(function () {
$.ajax({
url: 'Mailing/exportar.php',
type: 'POST',
data: {dados: JSON.stringify(dados), caminho: 'MailingTemp/qualquernomeOcidental2.csv', usuario: usuarioDivisao},
beforeSend: function () {
$(".mailing #aguarde_pequena2").fadeOut(200);
$(".mailing #aguarde_pequena").fadeIn(200);
},
success: function (data) {
alert(data);
}
});
});
Try to set the type of data you are passing in ajax: dataType: 'json'
– Luis Henrique
Tries to send directly as an array, without using the
JSON.stringify
.– Oeslei
Man if you want to pick up via
$_POST
php must quote values in js. Ex:{"dados": dados, "caminho":caminho}
– periotto
I tried everything you said , but it did not, thanks! But I gave an Alert in the array and it is coming empty before going to ajax requisão, could the problem be this ? He is sending php an empty array, but it fills normal, only at the time of sending it appears empty.. Any idea ?
– Marlon Castro
Periotto, I’ve done this before with a simple array, the difference now that this array is more complex. I’m picking with the
GetPost
, I’m doing so in php:$arrayDados = json_decode(stripslashes(GetPost('dados')));
– Marlon Castro
Shows the whole code where vc mounts the array(for, while, etc)
– periotto
Beauty, I’ll edit the question!
– Marlon Castro
Man you’re wearing
dados.lenght
.Thefor
will never run pq there is no record in the array– periotto
Precisely , if there is no record in the array and it adds , if it does not replace.
– Marlon Castro