每当用户点击“赞”按钮时,我都会更新数据库。更新已成功完成,但问题在于从数据库更新新获取的值。
ajax发布数据的控制函数:
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->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$result = $this->db->get();
return $result->result_array();
我成功的Ajax功能就是这样:
success: function(){
alert("Success");
$(this).addClass('.up_arrow').removeClass('.up_arrowed');
$('.rep_count').html(data);
}
有什么问题?我很困惑。
编辑 这是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);
}
});
}
});
});
答案 0 :(得分:1)
更改,
$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$result = $this->db->get();
return $result->result_array();
要,
$this->db->select('repo');
$this->db->from('restaurants');
$this->db->where('id', $id);
$result = $this->db->get();
$row=$result->result_array();
return $row['repo'];
和
success: function(){
alert("Success");
$(this).addClass('.up_arrow').removeClass('.up_arrowed');
$('.rep_count').html(data);
}
要,
success: function(data){
alert("Success");
$(this).addClass('.up_arrow').removeClass('.up_arrowed');
$('.rep_count').html(data);
}
您忘记将数据传递给成功功能。
答案 1 :(得分:1)
您需要将数据传递给成功函数,如下所示:
success: function(data){
所以完整的ajax成功回调函数将是:
success: function(data){
alert("Success");
$(this).addClass('.up_arrow').removeClass('.up_arrowed');
$('.rep_count').html(data);
}
另外:
$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
答案 2 :(得分:1)
尝试:
$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>";
更改:
url: "<?=base_url('restaurants/plusrepo')?>",