Rodolfo Oliveira
Rodolfo Oliveira

Reputation: 235

Problems with jQuery and JSON

I am trying to query a MySQL database and put the result in jQuery using JSON, because I need to do graphics.

My problem is when I manipulate the JSON.

My code at the moment: index.php

$(function ($) {

    $("#formulario").submit(function () {

        var nome = $("#nome").val();

        $("#status").html("<img src='loader.gif' alt='Enviando' />");
        $.post('envia.php', {
            nome: nome
        }, function (resposta) {
            $("#status").slideDown();
            if (resposta != false) {
                $("#status").html(resposta);
            } else {
                $("#nome").val("");
            }
        });
    });
});

envia.php (the query is only for test)

require_once("conecta.php");

$nome = $_POST["nome"];
$culturas = array();
$resultado = mysqli_query($conexao, "select *from mandioca_iea where  mandioca_iea_id=1");

while ($cultura = mysqli_fetch_assoc($resultado)) {
    array_push($culturas, $cultura);
}

echo json_encode($culturas);

If I test the code in this way: In div="status" appears:

[{"mandioca_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"}] OK,It works

But when I try to access the "fields" using this code, it doesn't work:

$.post('envia.php', {nome: nome}, function(resposta) {
    for(var i=0; i<resposta.length; i++) {
        var registro = resposta[i];
        console.log(registro.man_ins_area_10); 
    }
});

OBS: I tried resposta = $.parseJSON(resposta) too.

In console of web browser, the result is:

228 undefined, in other words, happens "loop" 228 times and all "vars" are undefined.

Does someone know why this happens?

Upvotes: 0

Views: 87

Answers (3)

Suchit kumar
Suchit kumar

Reputation: 11869

try with proper attribute.since there is no iea_id in your json data it is not giving anything.

 var resposta= [{"mandioca_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"}] 
     for(var i=0; i<resposta.length; i++) {
            var registro = resposta[i];
            console.log(registro.mandioca_iea_id); //it should give 1
        }

updated:

$.post('envia.php', {nome: nome}, function(resposta) {
    console.log(resposta);
    var resposta=JSON.parse(resposta);
    for(var i=0; i<resposta.length; i++) {
        var registro = resposta[i];
        console.log(registro.customerLat); 
    }
});

Upvotes: 1

Vijay
Vijay

Reputation: 3023

Stringify your post data parameter.

//stringify your json post data parameters
var postData=JSON.stringify({nome: nome});
$.post('envia.php', postData, function(resposta) {
    //parse received json string
    resposta= $.parseJSON(resposta);
    for(var i=0; i<resposta.length; i++) {
        var registro = resposta[i];
        console.log(registro.man_ins_area_10); 
    }
});

Upvotes: 0

Grushton94
Grushton94

Reputation: 613

edit: in addition to the comments "no iea_id field". can you try this? i added the "json" part

$.post('envia.php', {nome: nome}, function(resposta) {
            $("#status").slideDown();
            if (resposta != false) {
                $("#status").html(resposta);
            } 
            else {
                $("#nome").val("");
            }
},
"json" //this specifies that the result is a json object
);

Upvotes: 0

Related Questions