I have such a button, when I click on this button, no of likes will automatically change. Above the button, the number of liked ones is displayed. For this, I used ajax to implement this function.
This is my code in the view:
<p id='state'>
<i class='fa fa-thumbs-up'></i> <?php echo $countLike;?> likes •
<i class='fa fa-thumbs-down'></i> <?php echo $countDisLike;?> dislikes</p>
<button class='detailButton' name='like_name' id='click_like' value='<?php echo $link;?>' title='Like this post'><i class='fa fa-thumbs-up'></i> Like</button>
Javascript code above view:
<script type="javascript/text">
$(document).ready(function){
$("#click_like").click(function(event){
event.preventDefault();
var id = document.getElementById("click_name").value;
jQuery.ajax({
type:"GET",
url: "<?php echo base_url();?>index.php/photoCheese/like_total/",
dataType:'json',
data: {like_id = id},
success: function(res){
if(res){
jQuery("div#state").html(res.no_likes);
}
}
});
});
});
My controller:
public function like_total(){
$id = $this->session->userdata('userID');
$upload = $_GET['like_id'];
$data = array('like' => 1,
'userID'=>$id,
'uploadID'=>$_GET['like_id']);
$this->photoCheese_model->get_like_total($data,$upload);
return json_encode($data);
}
Model:
public function get_like_total($data,$uplaod){
$success = $this->db->insert('tbl_like',$data);
if($success){
$this->db->select('uploadID,SUM(`like`) as no_likes',false);
$this->db->where('uploadID',$upload);
$this->db->where('like !=',2);
$query = $this->db->get();
}
return $query->result_array();
}
When I try to run this code, it does not work. I mean, it shows no result. I am new to ajax and this is the first time I have taken advantage of this. Please help me with this. Many thanks.