简体   繁体   中英

codeigniter return fetch data from controller to view via Ajax request

I load the view in Which I want to display fetch record from the database through the ajax JSON request.But it's not showing the record.

Here is my view code

<div class="col-md-6" id="hodm_table">
<table class="table table-striped table-hover table-responsive">
  <thead>
    <tr>
      <th>Task Name</th>
      <th>Director</th>
      <th>Duration</th> 
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
     <?php foreach($result as $hodm) { ?>
          <tr>
          <td><?php echo $hodm->t_name;?></td>
          <td><?php echo $hodm->director;?></td>
          <td><?php echo $hodm->duration;?></td>
          <td><?php echo $hodm->status;?></td>
      <?php } ?>
  </tbody>
</table> 
</div>
</div>
<script type='text/javascript' language='javascript'>
$(document).ready(function(){
$.ajax({
url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table",
type: 'POST',
dataType: 'JSON',

success:function (data) {
  $('#hodm_table').html(data);
}

});
event.preventDefault();
});
</script>

Here is my model

public function get_hodm()
     {
          return $this->db->get("hodm");
     }

Here is my controller

public function dig_short_hodm_table(){

        $data['result']=$this->pojo->get_hodm();

        return json_encode($data);

     }

When I load my page then it showing the error

Message: Undefined variable: result

I want to when view load it fetch the record from the database and show in the view tables.

Update your model:

public function get_hodm(){
      return $this->db->get("hodm")->result();
}

Your controller:

    public function dig_short_hodm_table(){    
    $result_html = '';
    $result_set = $this->pojo->get_hodm();

    foreach($result_set as $result) {
        $result_html .= '
            <tr>
                <td>' . $result->t_name . '</td>
                <td>' . $result->director . '</td>
                <td>' . $result->duration . '</td>
                <td>' . $result->status . '</td>
            </tr>';                   

    }

    echo json_encode($result_html);
}

Finally your view:

<div class="col-md-6" id="hodm_table">
    <table class="table table-striped table-hover table-responsive">
        <thead>
            <tr>
                <th>Task Name</th>
                <th>Director</th>
                <th>Duration</th> 
                <th>Status</th>
            </tr>
        </thead>
        <tbody id="hodm_results">

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


<script type='text/javascript' language='javascript'>
    $(document).ready(function(){
        $.ajax({
            url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table",
            type: 'POST',
            dataType: 'JSON',

            success:function (data) {
                $('#hodm_results').html(data);
            }
        });

        event.preventDefault();
    });
</script>

you must change like this

public function get_hodm()
 {
      return $this->db->get("hodm")->result();
 }

I have update Model View and Controller:

<script type='text/javascript' language='javascript'>
$(document).ready(function(){
$.ajax({
    url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table",
    type: 'POST',
    dataType: 'JSON',

    success:function (data) {
        var result = data.result;'
        var row = "";
        for(i=0; i < result.length; i++) {
            row += "<tr>";
            row += "<td>"+result[i].t_name+"</td>";
            row += "<td>"+result[i].director+"</td>";
            row += "<td>"+result[i].duration+"</td>";
            row += "<td>"+result[i].status+"</td>";
            row += "</tr>";
        }
        $('#hodm_table > tbody').html(row);
    }

});
event.preventDefault();
});
</script>

Here is my model

public function get_hodm()
{
    $query = $this->db->get("hodm");
    return $query->result_array();
}

Here is my controller

public function dig_short_hodm_table(){

    $data['result']=$this->pojo->get_hodm();

    echo json_encode($data);

}

try to return in controller like this by changing header content

public function dig_short_hodm_table(){
header('Content-Type: application/x-json; charset=utf-8');
$data['result']=$this->pojo->get_hodm();
echo(json_encode($data));
}

Change Your Model Like Below

public function get_hodm()
{
    $query = $this->db->get("hodm");
    return $query -> result();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM