Reputation: 3443
I am updating the database whenever a user clicks the like button. The update is successfully done but the problem is in updating the new fetched value from the database.
Control Function on which ajax is posting data :
public function plusrepo()
{
if($this->input->is_ajax_request())
{
$this->load->model('themodel');
$rep['updated'] = $this->themodel->addrepo($this->input->post('resid'));
echo $rep['updated'][0]." <span>Reputation</span>";
}
}
This is how i am selecting from table and returning the result array.
$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$result = $this->db->get();
return $result->result_array();
My Ajax function on success does this :
success: function(){
alert("Success");
$(this).addClass('.up_arrow').removeClass('.up_arrowed');
$('.rep_count').html(data);
}
What is wrong? I'm confused.
Edited This is the complete function for ajax
$('.up_arrow').each(function() {
$(this).click(function(event) {
event.preventDefault();
var resid = $(this).attr('name');
var post_data = {
'resid' : resid,
'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
};
if(resid){
$.ajax({
type: 'POST',
url: "/ci_theyaw/restaurants/plusrepo",
data: post_data,
success: function(data){
console.log(data);
// alert(data);
// $(this).addClass('.up_arrow').removeClass('.up_arrowed');
// $('.rep_count').html(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
console.log(xhr.responseText);
alert(thrownError);
}
});
}
});
});
Upvotes: 0
Views: 1760
Reputation: 6948
You need to pass data to success function like this :
success: function(data){
So full ajax success callback function would be :
success: function(data){
alert("Success");
$(this).addClass('.up_arrow').removeClass('.up_arrowed');
$('.rep_count').html(data);
}
Also :
$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$this->db->limit(1);
$result = $this->db->get();
print_r($result);//For testing
//echo $result['repo']; // For working code
Upvotes: 1
Reputation: 7475
Try:
$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$result = $this->db->get()->row_array();
return $result['repo'];
echo $rep['updated']." <span>Reputation</span>";
Change :
url: "<?=base_url('restaurants/plusrepo')?>",
Upvotes: 1
Reputation: 23483
Change,
$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$result = $this->db->get();
return $result->result_array();
To,
$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$result = $this->db->get();
$row=$result->result_array();
return $row['repo'];
And,
success: function(){
alert("Success");
$(this).addClass('.up_arrow').removeClass('.up_arrowed');
$('.rep_count').html(data);
}
To,
success: function(data){
alert("Success");
$(this).addClass('.up_arrow').removeClass('.up_arrowed');
$('.rep_count').html(data);
}
Here you forgot to pass data to success function.
Upvotes: 1