codeigniter return fetch data from controller to view via Ajax request

11,370

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>
Share:
11,370
iazizkhan
Author by

iazizkhan

Updated on June 13, 2022

Comments

  • iazizkhan
    iazizkhan almost 2 years

    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.

  • Muhamad Riyan
    Muhamad Riyan about 7 years
    tell me what the problem or message?
  • Muhamad Riyan
    Muhamad Riyan about 7 years
    i know the problem you just change $data['result'] with $data
  • iazizkhan
    iazizkhan about 7 years
    first this message Message: Undefined variable: result blink then it show blank page
  • Muhamad Riyan
    Muhamad Riyan about 7 years
    oh change this line to return json_encode($data); into echo json_encode($data);
  • Muhamad Riyan
    Muhamad Riyan about 7 years
    what do you change and what the message, and do you know console? to detect your fault?
  • theEUG
    theEUG about 7 years
    Can you give me a descriptive error message that you are getting?
  • iazizkhan
    iazizkhan about 7 years
    Undefined variable: result this are showing and in console it showing blank
  • iazizkhan
    iazizkhan about 7 years
    A PHP Error was encountered Severity: Notice Message: Undefined variable: result Filename: digital/dashboard.php Line Number: 38 Backtrace: File: C:\xampp\htdocs\tmt_project\application\views\digital\dashbo‌​ard.php Line: 38 Function: _error_handler File: C:\xampp\htdocs\tmt_project\application\controllers\digital\‌​dashboard.php Line: 17 Function: view File: C:\xampp\htdocs\tmt_project\index.php Line: 292 Function: require_once
  • Muhamad Riyan
    Muhamad Riyan about 7 years
    do you change $data['result'] into $data ? on your controller?
  • iazizkhan
    iazizkhan about 7 years
    yes I change but same error showing and then page goes blank
  • Muhamad Riyan
    Muhamad Riyan about 7 years
    maybee you must set on your ajax console.log(data); on success section right
  • iazizkhan
    iazizkhan about 7 years
    I didn't get you
  • theEUG
    theEUG about 7 years
    I'll add another answer give me a few seconds
  • iazizkhan
    iazizkhan about 7 years
    Ok, Thank you. I am waiting
  • Muhamad Riyan
    Muhamad Riyan about 7 years
    what your mean? console.log(data) is to detect, data and type from controller
  • iazizkhan
    iazizkhan about 7 years
    Object result : Array(58) 0 : Object date : "2016-09-30" director : "Ameesha Mam" duration : "0.7" id : "6" partner : "suneet" status : "Done" t_name : "LI-Emailers" task : "Working on LI Emailers with suneet for triggering as suggested by ameesha mam" update_by : "" updated_at : "0000-00-00 00:00:00" user_name : "om" proto : Object console output
  • iazizkhan
    iazizkhan about 7 years
    $result_html .= ' <tr> <td>' . $result->t_name . '</td> <td><?php echo $result->director . '</td> <td><?php echo $result->duration . '</td> <td><?php echo $result->status . '</td> </tr>'; I think there is some mistakes
  • iazizkhan
    iazizkhan about 7 years
    Please click up to my question . I already mark your answer.. Thanx for help
  • iazizkhan
    iazizkhan about 7 years
    I did . Please click up for my question also
  • theEUG
    theEUG about 7 years
    @xr33dx You need to click on the Right Mark below the downvote button to accept as answer
  • Muhamad Riyan
    Muhamad Riyan about 7 years
    the meaning is your success from controller into view, it just looping on view like this $.each(data, function(index, val) {$('#hodm_table').append(val.director)});
  • iazizkhan
    iazizkhan about 7 years
    Yes I did, Please click my question also
  • Gaurav
    Gaurav about 7 years
    Can you check in console? what response are you getting
  • iazizkhan
    iazizkhan about 7 years
    I got the answer thanks for help. I prefer theEUG answer
  • Jasshh Andrews
    Jasshh Andrews almost 5 years
    I'm only getting the last value from the database ?