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>
Author by
iazizkhan
Updated on June 13, 2022Comments
-
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 about 7 yearstell me what the problem or message?
-
Muhamad Riyan about 7 yearsi know the problem you just change $data['result'] with $data
-
iazizkhan about 7 yearsfirst this message Message: Undefined variable: result blink then it show blank page
-
Muhamad Riyan about 7 yearsoh change this line to return json_encode($data); into echo json_encode($data);
-
Muhamad Riyan about 7 yearswhat do you change and what the message, and do you know console? to detect your fault?
-
theEUG about 7 yearsCan you give me a descriptive error message that you are getting?
-
iazizkhan about 7 yearsUndefined variable: result this are showing and in console it showing blank
-
iazizkhan about 7 yearsA 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\dashboard.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 about 7 yearsdo you change $data['result'] into $data ? on your controller?
-
iazizkhan about 7 yearsyes I change but same error showing and then page goes blank
-
Muhamad Riyan about 7 yearsmaybee you must set on your ajax console.log(data); on success section right
-
iazizkhan about 7 yearsI didn't get you
-
theEUG about 7 yearsI'll add another answer give me a few seconds
-
iazizkhan about 7 yearsOk, Thank you. I am waiting
-
Muhamad Riyan about 7 yearswhat your mean? console.log(data) is to detect, data and type from controller
-
iazizkhan about 7 yearsObject 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 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 about 7 yearsPlease click up to my question . I already mark your answer.. Thanx for help
-
iazizkhan about 7 yearsI did . Please click up for my question also
-
theEUG about 7 years@xr33dx You need to click on the Right Mark below the downvote button to accept as answer
-
Muhamad Riyan about 7 yearsthe 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 about 7 yearsYes I did, Please click my question also
-
Gaurav about 7 yearsCan you check in console? what response are you getting
-
iazizkhan about 7 yearsI got the answer thanks for help. I prefer theEUG answer
-
Jasshh Andrews almost 5 yearsI'm only getting the last value from the database ?