in code, after clicking button, data added in database already. wanted after adding data, query total of chosen item , display without loading page.
this code now:
my view:
<p id='state'><i class='fa fa-thumbs-up'></i><span id="likethis"><?php echo $countlike;?></span> likes • <i class='fa fa-thumbs-down'></i><?php echo $countdislike;?> dislikes •<i class='fa fa-thumbs-down'></i><a href='<?php echo base_url();?>index.php/photocheese/deleteuploadpic/<?php echo $row['uploadid'];?>'>delete picture</a></p> <input type="button" onclick="getval(this.value)" class='detailbutton1' name='like_name' id='like_id' value='<?php echo $link;?>' title='like post'><i class='fa fa-thumbs-up'></i> like</input>
javascript:
function getval(value) { jquery.ajax({ type:"get", url: "<?php echo base_url();?>index.php/photocheese/like_total/", datatype:'json', data: {like_id : value}, success: function(res){ alert(res.no_likes); if(res){ jquery("#likethis").html(res.no_likes); }
} });
controller:
public function like_total(){ $id = $this->session->userdata('userid'); $upload = $this->input->get('like_id'); $data = array('like' => 1, 'userid'=>$id, 'uploadid'=>$_get['like_id']); $result = $this->photocheese_model->get_like_total($data,$upload); return json_encode($result); }
model:
public function get_like_total($data,$uplaod){ $success = $this->db->insert('tbl_like',$data); if($success){ $this->db->select('uploadid,sum(`like`) no_likes',false); $this->db->where('uploadid',$upload); $this->db->where('like !=',2); $query = $this->db->get(); } return $query->result_array(); }
this code not display total_likes. what's wrong one?
this looks decent code me
public function like_total(){ $id = $this->session->userdata('userid'); $upload = $this->input->get('like_id'); $data = array('like' => 1, 'userid'=>$id, 'uploadid'=>$_get['like_id']); $result = $this->photocheese_model->get_like_total($data,$upload); return json_encode($result); }
just 1 try.. change
return json_encode($result)
to
echo json_encode($result)
this example may in future jquery ajax php example
Comments
Post a Comment