I have a views column in my database. And I want to update the column with +1 when a hyperlink is clicked to visit a certain page. Am working with jQuery, Ajax and codeigniter. I an not able to do the correct thing so as to trigger update in the database. Please assist me with what am missing.
Below are my codes:
HTML
<a href="<?php echo base_url(); ?>site/details/<?php echo $value->hid; ?>" name="hid" id="clicker">
JS
$(document).ready(function(){
$("a#clicker").click(function(){
$.ajax({
type: "POST",
url: "<?php base_url(); ?>site/traffic",
success: function(data){
alert("I got a view");
console.log(data);
}
});
});
});
Controller
public function traffic(){
$id = $this->input->post("hid");
$this->My_model->updateView($id);
}
Model
public function updateView($id = NULL, $data){
$this->db->set('view', 'view+1');
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}