0

每当用户单击“赞”按钮时,我都会更新数据库。更新已成功完成,但问题在于更新从数据库中获取的新值。

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);
                }
            });
        }
    });
});
4

3 回答 3

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);
     }

在这里,您忘记将数据传递给成功函数。

于 2013-09-18T11:44:52.903 回答
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
于 2013-09-18T11:47:15.990 回答
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')?>",
于 2013-09-18T11:47:48.213 回答