簡體   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