dongtongjian3127 2015-06-26 18:14
浏览 27
已采纳

Codeigniter使用ajax在我的网络应用程序中的类似按钮上需要帮助

I have been working in app that contains a like button in profiles I have stopped here and don't know how to complete it!! What is missed here to make it work? can you help ?

Ajax

<script type="text/javascript">
$(document).ready(function() {
$("#likebtn").click(function(e){

$.post(

     function(data) 
     {

     if (data.st == 0)
     {
     $('#likedata').html(data.msg);
     }
           else if (data.st == 1)
     {
         <?php echo $numlikes+1; ?>
     }
     }, 
     'json'
   );
 return false;   
 });


});

</script>

Button in the View

  <p> <input type="button" id="likebtn" class="btn btn-default btn-lg"> Likes</p>
  <span class="label label-default" id="Likes"><?php echo $numlikes; ?></span>
  <div id="likedata"></div>

on construct function of the controller

 function __construct() {
    parent::__construct();
                $prifile_id = $this->uri->segment(2, 9);
                $user_id = ($this->session->userdata['logged_in']['user_id']);

 }

Controller function to add like

 public function addlike()
    {
        $checklike = $this->$this->profiles_model->checklike($user_id,$profile_id);
        if ($checklike == FALSE)
        {
        $this->profiles_model->addlike($user_id,$prifile_id);
        $output= array('st'=>1);
     echo json_encode($output);
        }
        else {

             $output = array('st'=>0, 'msg' => "you already likes this profile");

         echo json_encode($output);
        }
    }

Thanks in advance :)

  • 写回答

1条回答 默认 最新

  • dpvhv66448 2015-06-26 18:25
    关注

    You want to increment count, but it is good to get updated count from server. Following is some javascript changes.

     <script>  
       $.post(
         "<?php echo site_url('profiles/addlike'); ?>",
         function(data) 
         {
    
         if (data.st == 0)
         {
         $('#likedata').html(data.msg);
         }
               else if (data.st == 1)
         {
             $("#Likes").text(st.numLikes);
         }
         }, 
         'json'
       );
     return false;   
     });
    
    
    });
    
    </script>
    

    Now, Return updated numofLikes from server too.

    public function addlike()
        {
            $checklike = $this->$this->profiles_model->checklike($user_id,$profile_id);
            if ($checklike == FALSE)
            {
            // Update this model method, so that it returns updated like count.
            $numLikes = $this->profiles_model->addlike($user_id,$prifile_id);
            $output= array('st'=>1,'numLikes'=>$numLikes);
         echo json_encode($output);
            }
            else {
    
                 $output = array('st'=>0, 'msg' => "you already likes this profile");
    
             echo json_encode($output);
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?