0

I have table in sales activity log like below

sales activity

id |    activity          |      userid     |     companyid         |     dateadded
 1   Set Meeting 3pm                2                 1               2019-12-26 10:29:59
 2   Send Proposal                  2                 1               2019-12-27 11:15:04
 3   Meeting                        3                 2               2019-12-27 13:01:13

I want to show activity log based on client name with modal popup, but the result is undefined.

Here's the code for js

 <script type="text/javascript">
 $('a[href="#myHistory"]').on('click',function(){

var id = $(this).attr('id');

$.ajax({
    type : "POST",
    url  : "<?php echo admin_url(). 'sales/get_history'; ?>",
    data : {id:id},
    success : function(data){
        var html = '';
        var i;
        for(i=0; i<data.length; i++){
            html += '<tr>'+
                        '<td>'+data[i].description+'</td>'+
                        '<td>'+data[i].dateadded+'</td>'+
                    '</tr>';
        }
        $('#show_data').html(html);
    }
})
})
</script>

Here's my controller

public function get_history(){
    $code=$this->input->post('id');
    $data=$this->sales_model->get_history_by_code($code);
    echo json_encode($data);
}

Here's my model

function get_history_by_code($code)
{
    $abc=$this->db->query("SELECT * FROM tblsales_log WHERE companyid='$code'");
    if($abc->num_rows()>0){
        foreach ($abc->result() as $data) {
            $result=array(
                'description'      => $data->description,
                'dateadded'        => $data->dateadded,
            );
        }
    }
    return $result;
}

Here's my view

  <div class="modal" id="myHistory" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            <table id="example" class="table table-striped table-bordered dt-responsive nowrap" style="width:100%">
                <thead>
                    <tr>
                        <th>Description</th>
                        <th>Added Time</th>
                    </tr>
                </thead>
                <tbody id="show_data">

                </tbody>
            </table>
        </div>
    </div>
</div>

When the modal popup, it's only show undefined. I have tried console.log, but the result is still undefined. Do you know where's the error of my code ?

Thank you

1
  • You have to parse the data as json data using JSON.parse(data) and then loop over. Commented Dec 31, 2019 at 4:38

2 Answers 2

1

In your ajax response you have to parse JSON.

$.ajax({
type : "POST",
url  : "<?php echo admin_url(). 'sales/get_history'; ?>",
data : {id:id},
success : function(data){

var data = JSON.parse(data);
  console.log(data);
    var html = '';
    var i;
    for(i=0; i<data.length; i++){
        html += '<tr>'+
                    '<td>'+data[i].description+'</td>'+
                    '<td>'+data[i].dateadded+'</td>'+
                '</tr>';
    }
    $('#show_data').html(html);
}

}) })

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

Comments

0

update your model code

initialize array in first line and

push result in array

$result[]=array(
       'description'      => $data->description,
       'dateadded'        => $data->dateadded,
);

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.