0

I am receiving data in JSON format for name & email by $.ajax() jquery function. I need to put this data in tabular format in a div ('#div1'). I am unable to do this. Can someone help?

JSON received:

[{"jname":"Charles","jemail":"[email protected]"},{"jname":"Bollen","jemail":"[email protected]"},{"jname":"Sita","jemail":"[email protected]"},{"jname":"Lita","jemail":"[email protected]"},{"jname":"Azma","jemail":"[email protected]"},{"jname":"Robert","jemail":"[email protected]"},{"jname":"Charu","jemail":"[email protected]"}]

jQuery Ajax Code:

$.ajax({
    type: "POST",
    url: "retrieve.php",
    data: jQuery("#form1").serialize(),
    cache: false,
    success:  function(data){
       $('#div1').html(data);
    }
});

Please, help. I need to print this data in tabular form in a div for Name & Email.

2
  • Do you really need help building a HTML table..? Commented Aug 12, 2015 at 7:03
  • I need to know how to put this json data in tabular format. Commented Aug 12, 2015 at 7:05

3 Answers 3

2

What about using a table? Something like this:

 $.ajax({
    type: "POST",
    url: "retrieve.php",
    data: jQuery("#form1").serialize(),
    cache: false,
    success:  function(data){
       var html = '<table>';
       JSON.parse(data).forEach(function(item){ 
           html += '<tr><td>'+item.jname+'<td><td>'+item.jemail+'</td></tr>'; 
       });
       html += '</table>';
       $('#div1').html(html);
}

});

Sign up to request clarification or add additional context in comments.

Comments

1

Put your JSON data into table rows, don’t forget the <tbody>:

var HTML = '';
var JSON = [{"jname":"Charles","jemail":"[email protected]"},
            {"jname":"Bollen","jemail":"[email protected]"}];
JSON.forEach(function(person){
    HTML += '<tr><td>' + person.jname + '</td><td>' + person.jemail + '</td></tr>';
});
document.body.insertAdjacentHTML('afterbegin','<table><tbody>' + HTML + '</tbody></table>');

Comments

0

You need to parse that.

For example:

$.ajax({
type: "POST",
url: "retrieve.php",
data: jQuery("#form1").serialize(),
cache: false,
success:  function(data){
   var parse = JSON.parse(data);
   //then do what you are going to do.
   alert(parse.jname);
   alert(parse.jemail);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.