How to load datra on button click using ajax call?

How to load div content on button click using ajax with db.

File name : controller.php

public function showcomments()
{
//$leader_id = $this->uri->segment(3);
$leader_id = $this->input->get('id');
$commentsresult = $this->Auth_model->getcomments_info($leader_id);
$data['commentsdata'] = $commentsresult;
//load the home view
$comment_html = $this->load->view('comments',$data,true);
echo $comment_html;
exit;

}

View Page

File name : view.php

<html>
<head>

</head>
<script type="text/javascript">

$(function(){

$('.btn').click(function(){
var lid = this.id ;

$.ajax({
type:'GET',
data:'id='+lid,
url : '<?php echo base_url();?>home/showcomments',
success:function(result){
//alert(response);
$("#comment_list").html(result);

},
error: function(msg){

alert("error");
}


});


});
});



</script> <body>
<a href="javascript:void(0)" class="btn"
id="<?php echo $row->id; ?>">Show Comments</a>

<div id="comment_list">


</div>

</body>
</html>

File name : comments.php


<table class="table datatable">
<thead>
<tr>

<th>Comments By</th>
<th>Comments</th>

</tr>
</thead>
<tbody>

<?php
$kk = count ( $commentsdata );
if($kk > 0){
for($c = 0; $c < $kk; ++ $c) {
?>

<tr>
<td><?php echo $commentsdata[$c]->user_name; ?></td>
<td><?php echo $commentsdata[$c]->comments; ?></td>


</tr>
<?php }

}
else {

?>

<tr>
<td>No Comments</td>

</tr>
<?php }?>

</tbody>
</table>









Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here