动态变化的数据在codeIgniter使用Ajax动态、数据、Ajax、codeIgniter

2023-09-10 18:04:25 作者:囚禁自己

我有这样的类似按钮,当我点击这一块,不喜欢的会自动更改。显示按钮上方喜欢的数目。要做到这一点,我已经使用AJAX来实现此功能。

I have this like button, when I click this one, the no of likes will change automatically. The number of likes is displayed above the button. To do this, I've used ajax to implement this function.

这是我的code。在查看:

This is my code in the View:

<p id='state'>
   <i class='fa fa-thumbs-up'></i> <?php echo $countLike;?> likes &bull; 
   <i class='fa fa-thumbs-down'></i> <?php echo $countDisLike;?> dislikes</p>

   <button class='detailButton' name='like_name' id='click_like' value='<?php echo $link;?>' title='Like this post'><i class='fa fa-thumbs-up'></i> Like</button>

Javascript的code中的上述观点:

Javascript code above the view:

<script type="javascript/text">
$(document).ready(function){
  $("#click_like").click(function(event){
    event.preventDefault();
    var id = document.getElementById("click_name").value;
    jQuery.ajax({
      type:"GET",
      url: "<?php echo base_url();?>index.php/photoCheese/like_total/",
      dataType:'json',
      data: {like_id = id},
      success: function(res){
        if(res){
          jQuery("div#state").html(res.no_likes);
        }      
      }
    });
  });
});

我的控制器:

public function like_total(){
        $id = $this->session->userdata('userID');
        $upload = $_GET['like_id'];
        $data = array('like' => 1,
                        'userID'=>$id,
                        'uploadID'=>$_GET['like_id']);

        $this->photoCheese_model->get_like_total($data,$upload);

        return json_encode($data);
    }

型号:

public function get_like_total($data,$uplaod){
        $success = $this->db->insert('tbl_like',$data);

        if($success){
            $this->db->select('uploadID,SUM(`like`) as no_likes',false);
            $this->db->where('uploadID',$upload);
            $this->db->where('like !=',2);

            $query = $this->db->get();


        }
        return $query->result_array();
    }

当我尝试运行此code,它没有任何效果。我的意思是,它表明没有结果的。我是新来的阿贾克斯,这是我第一次也是为了实现这一个。请帮我这一个。非常感谢。

When I try to run this code, it has no effect. I mean, it shows no result at all. I'm new to ajax and it is my first time also to implement this one. Please, help me with this one. Thanks a lot.

推荐答案

象下面这样使用更改数据:到位 =

change data like below use : in place of =

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

jQuery.ajax({
      type:"GET",
      url: "<?php echo base_url();?>index.php/photoCheese/like_total/",
      dataType:'json',
      data: {like_id : id},
      success: function(res){

          $("#like").html(res);

      }
    });

和您的 HTML 用于显示像数如下图所示。

and your HTML for showing like count like below

<p id='state'>
   <i class='fa fa-thumbs-up'></i> <b id="like"><?php echo $countLike;?></b> likes &bull; 
   <i class='fa fa-thumbs-down'></i> <?php echo $countDisLike;?> dislikes</p>
 
精彩推荐
图片推荐