繁体   English   中英

使用Ajax从数据库更新页面上的值

[英]Updating value on page from database using ajax

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

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

更改,

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

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

您需要像这样将数据传递给成功函数:

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

尝试:

$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')?>",

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM