Query the database send and use javascript

Asked

Viewed 1,952 times

1

My difficulty is after sending to java script how to separate each record:

Example: In my code I’m running this function:

$culturas=array();
        $resultado=mysqli_query($conexao,"select *from mandioca_iea where id_cult=1");
            while($cultura=mysqli_fetch_assoc($resultado))
            {
                array_push($culturas,$cultura);
            }
        echo json_encode($culturas);

And to receive the consultation:

$.post('envia.php', {nome: nome}, function(resposta) {}

So far so good, he sends Javascript something like this:

On my console returned this :[{"iea_id":"1","man_ins_area_00":"0","man_ins_prod_00":"0","man_ins_area_05":"0"‌​, "man_ins_prod_05":"0","man_ins_area_10":"0","man_ins_prod_10":"0","man_ins_area_‌​13":"0", "man_ins_prod_13":"0","id_cid":"1","id_cult":"1"}, {"iea_id":"2","man_ins_area_00":"0","man_ins_prod_00":"0","man_ins_area_05":"0", "man_ins_prod_05":"0","man_ins_area_10":"0","man_ins_prod_10":"0", "man_ins_area_13":"0","man_ins_prod_13":"0","id_cid":"2","id_cult":"1"}]

Where :"iea_id":"1" and "iea_id":"2",are two different records.

My doubt is if there is any way access the "records" (because in this format are no longer records),separately,as if it were a vector,for mounting a chart?

EDITING WITH THE ERROR: inserir a descrição da imagem aqui

Complete code being used:

<script type="text/javascript" language="javascript">
$(function($) {
    $("#formulario").submit(function() {
        $("#status").html("<img src='loader.gif' alt='Enviando' />");
        $.post('envia.php', {nome: nome}, function(resposta) {
    for(var i=0; i<resposta.length; i++) {
        var registro = resposta[i];
        console.log(registro.iea_id);
    }
});
    });
});
</script>

1 answer

4


You have an array of objects. Brackets define the array, and each pair of { and } sets an object (one for each record). You access the records by looping the array:

$.post('envia.php', {nome: nome}, function(resposta) {
    for(var i=0; i<resposta.length; i++) {
        var registro = resposta[i];
        console.log(registro.iea_id); // 1 na primeira passada, 2 na segunda
    }
});

It’s that simple, inside the loop you can access the fields by name/key.

As for the chart setup, you can reprocess the data in Javascript and reorganize it to pass to the graphics generator, or already pass in the right format since PHP. It depends on whether or not you need the complete data (and organized by record) in JS.

  • When I run the code on my console appears only:166 Undefined. On this line: console.log(record.iea_id); // 1 in the first step, 2 in the second, has an idea of what it is?

  • The data are those that are in question even? It seems that the first id comes 166, and the second blank.

  • The data are those, only I put 2 only, in vdd it returns +- 600 records, I will edit the post with an error picture for you to better understand

  • Edited as full code and error image

  • you can give a console.log(typeof resposta)?

  • Check this error:" Uncaught Typeerror: Illegal Invocation"

  • Are you sure you wrote as in my comment? This error there should not be generated by the code I mentioned.

  • I put this:$("#status"). html("<img src='Loader.gif' alt='Sending' />"); $. post('send.php', {name: name}, Function(reply) { console.log(typeof reply); . O erro que deu foi esse: Uncaught TypeError: Illegal invocationjquery.js:6 x.param.ojquery.js:6 gnjquery.js:6 gnjquery.js:6 gnjquery.js:6 x.paramjquery.js:6 x.extend.ajaxjquery.js:6 x.each.x.(anonymous function)index2.php:43 (anonymous function)jquery.js:5 x.event.dispatchjquery.js:5 x.event.add.v.Handle

  • Dude, I don’t know what could be going on, try a resposta = $.parseJSON(resposta) before the loop.

  • To work the only library you need is jquery né?

  • Yeah, just the jQuery...

Show 6 more comments

Browser other questions tagged

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