1

Hi I Want to draw a table with json data which is my code I am getting correct json response but not able to draw a table in popup, Which is my Code

$(document).on('click','.details', function(){
var student_id = $(this).attr("id");
//alert(Id);
    $.ajax({
      url: "<?php echo base_url(user/details);?>",
      type: "post",
      data: {id:id},
      success: function(data)
            { 
    $.each(json, function (i,data) {
        $("#fees_table").append( "<tr><td>" + data.name + "</td><td>" + data.first_name + "</td><td>" + data.last_name + "</td><td>" + data.user_name + "</td></tr>");
    });

            }
    });
 } );

Which is my controller And My model is

public function getStudentFees($student_id)
{
    $query=$this->db->select('name,first_name,last_name,user_name')->from('users');
    $result= $query->result_array();
    //echo $this->db->last_query();die();
    return $result;
}

2 Answers 2

1

Change this

$.each(json, function (i,data) {
    //....
});

as

var data = $.parseJSON(data);
$.each(data, function (i) {
    $("#fees_table").append( "<tr><td>" + data[i].name + "</td><td>" + data[i].first_name + "</td><td>" + data[i].last_name + "</td><td>" + data[i].user_name + "</td></tr>");
});

If you getting correct response then it should work

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

9 Comments

Thank you @Rejoanul Alam, But I am getting Uncaught Reference Error: json is not defined
Uncaught TypeError: Cannot use 'in' operator to search for '516' in
then you should parse your json data, as you are getting json string. check my edited answer
excellent @rejoanul, Now Table is coming but json data also showing correct in console but In table the fields are showing undefined. Anything wrong??
then show your console.log(data) values & details method code of user controller
|
0

add dataType to your ajax options

 $.ajax({
      url: "<?php echo base_url(user/details);?>",
      type: "post",
      data: {id:id},
dataType: "json", // convert response data to json 
      success: function(data)
            { 
    $.each(json, function (i,data) {
        $("#fees_table").append( "<tr><td>" + data.name + "</td><td>" + data.first_name + "</td><td>" + data.last_name + "</td><td>" + data.user_name + "</td></tr>");
    });

            }
    });

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.