Ajax is returning me an array and now, how to separate?

Asked

Viewed 2,179 times

1

My Ajax request is returning 3 records of a PHP query (yes, I’m sure), so we have arrays of the type:

array 1 ["João", "19/05/1986", "masculino", "Programador", "Campo Grande"]
array 2 ["Maria", "15/05/1988", "feminino", "Enfermeira", "Londres"]
array 3 ["Patricia", "04/11/1980", "feminino", "Servidora Pública", "Fortaleza"]

Question: How do I separate this data so that I can take the information of each array separately and continue my system? To help I’ll put a piece of code

$.ajax({
    type: "POST",
    dataType: "json",
    url: "<?php bloginfo('template_url'); ?>/scripts/php_functions.php",
    data: {
        idClienteDocumentos: idClienteDocumentos,
        ajax: "true"
    },
    success: function(result) {

            var resultado = JSON.parse(result);

            // preciso montar as arrays cujo contador me confirmou que existem
            $("#Contador").val(resultado[6]);

For those who feel free to give a little extra strength, I need to put this inside a while PHP.

  • What gives you typeof result and result.length?

  • You caught me because I’m still super beginner in jQuery and I don’t know how to get this information! It can help me to know how I get to this data?

  • 1

    No problem. Do it like this: alert('Tipo: ' + typeof resultado + ', length: ' + resultado.length); on the line below var resultado = JSON.parse( etc...

  • 1

    I found this in the OS en foreach in Javascript

  • Type: string Lenght: 83 ....

  • 1

    Hmmm this after JSON.parse? using resultado in the Alert?

  • Right after JSON Alert(result);

  • 1

    can you put the code you have in php that echo to ajax? something is wrong there I think

  • Why does your PHP code return an array instead of objects (people, I imagine)?

  • http://pastebin.com/6f3q7kBC ... this is the PHP code I’m using to fetch the results that are returning the arrays. This is the image I have of Alert: http://puu.sh/aEhSh/035bdea5ad.jpg ... This is the return of the console: http://puu.sh/aEhVN/1f0832ef96.png. (has 4 records) which I obtained using this code: http://puu.sh/aEi1V/76f478677f.png

  • Daguilherme, sometimes for lack of knowledge I expressed myself badly, it may be that it is returning objects even.

  • @marcosvinicius but then made the Alert before from JSON.parse, I wrote "in the line below". So it’s better

  • @Sergio, excuse me, you’re right. I just tidied up and returned this: http://puu.sh/aEiaC/e1bc18564c.png

  • Good. Explain me one more question: what do you mean by " I need to put this inside a while PHP"?

  • I have a table with inserted files related to each user. So let’s say a user has 5 file records, I need a while to return the 5 records as shown in the image: http://puu.sh/aEikI/2535685976.png

  • So it means something like a while from PHP but in javascript/client side?

Show 12 more comments

1 answer

1


If you already have an array coming in after JSON.parse, you can use foreach to mount that table. If you want to do it with Divs here is a hint:

var campos = ['arquivo', 'idCliente', 'titulo', 'descricao', 'arquivo', 'data'];
var tabela = $('<div id="tabela"></div>');
resultado.forEach(function (linha) {
    var linhaDiv = $('<div></div>');
    linha.forEach(function (campo, index) {
        $('<div></div>').addClass(campos[index]).text(campo).appendTo(linhaDiv);
    });
    linhaDiv.appendTo(tabela);
});

It would be interesting if you sent objects directly from PHP, so the fields that will be classes could already come together. I suggest this at the end of this answer.

  • I am not able to make the code work this way (http://puu.sh/aEm2L/9c93ea474f.png) and I don’t know where it will enter table. Will it be automatically generated? Where is the script positioned? For now I have a Undefined error on the console. Moreover when I leave only Alert, it is giving 6 Alerts that corresponds to the amount of array elements and not records.

  • As @Sergio said, I transformed it into an object in PHP. Náo [and strange to be returning length: Undefined? http://puu.sh/aEnP2/7fa8c5d2fe.png

  • @marcosvinicius I’ve been on the outside. I see now you’ve had problems. Have I solved it? The example I gave above works like this: http://jsfiddle.net/f32SV/

  • I had trouble with PHP regarding json and had problems in some parts of ajax script as well. It has 5 minutes that I managed to return the data. Thank you.

  • 1

    @marcosvinicius ok, and now it’s working? with objects or array?

  • 1

    is working perfectly with the return of objects to ajax. thanks for the help. me adds on the face, http://fb.com/yesmarcos and also in Hangout: [email protected] ... Obrigaduuuuuuuuu

Show 1 more comment

Browser other questions tagged

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