akauts
akauts

Reputation: 153

in jquery .post need to format json array to html table

im newbie at all and learnt to move my old style coding to these. and now I need helps.

my JSON (array) - result from my php json_encode:

{"e_id":"12101","e_password":kkkk,"e_secretQuestion":null
{"e_id":"12102","e_password":kkkk,"e_secretQuestion":"abc"}
{"e_id":"12103","e_password":kkkk,"e_secretQuestion":"abc"}

my jquery:

e.preventDefault();
$.post("/general/helper.php?page=login",$(this).serialize(),function(data,status){          
    if (data != null){
        var tblheader = "<table><tr>";  
        var tblbody= "";    
        $.each(data, function(i, field){
            tblbody = tblbody + "<td style='border:1px solid gray'>" + field + "</td>";
        });
        tblbody = tblheader + tblbody + "</tr></table>";
        $("#hasil").html(tblbody);
    }
},"json");  

Question:

Its only format onto a table if return 1 row only, but if more rows the table is not formatted... pls help, how to simply format it to table? at this moment, please don't suggest me to use plugin to format json array to table.

thanks

Upvotes: 2

Views: 393

Answers (1)

Vyacheslav Voronchuk
Vyacheslav Voronchuk

Reputation: 2463

If your data is a parsed JSON array, when your code should look like that:

e.preventDefault();
$.post("/general/helper.php?page=login",$(this).serialize(),function(data,status){          
    if (data != null){
        var tblbody = "<table>";    
        $.each(data, function(i, row){
            tblbody += '<tr>';
            $.each(row, function(i, field) {
                tblbody += "<td style='border:1px solid gray'>" + field + "</td>";
            });
            tblbody += '</tr>';
        });
        tblbody += "</table>";
        $("#hasil").html(tblbody);
    }
},"json");  

Upvotes: 1

Related Questions