thinkphp+ajax局部刷新点赞

1. [代码]thinkphp点赞方法    

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public function zan(){

        $data['id']=isset($_POST['id'])?intval(trim($_POST['id'])):0;

        $obj = M("Article");

 

        if(!isset($_COOKIE[$_POST['id']+10000])&&$obj->where($data)->setInc('zan')){

            $cookiename = $_POST['id']+10000;

            setcookie($cookiename,40,time()+60,'/');

 

            $data['info'] = "ok";

            $data['status'] = 1;

            $this->ajaxReturn($data);

             

            exit();

        }else{

            $data['info'] = "fail";

            $data['status'] = 0;

            $this->ajaxReturn($data);

            exit();

        }

         

    }

2. [代码]js    

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

$(".z").on('click',function(){

        var Oa=$(this);

        var id=Oa.attr('id');//获取id属性

        var vl=Oa.find("span").text();

            vl=parseInt(vl)+1;

        $.post('__CONTROLLER__/zan',{id:id},function(data){

            if(data.status==1){

                alert('感谢您的支持!');//模拟异步数据加1

                Oa.find("span").text(vl);//页面元素加1

            }else{

                alert('您已经点过赞了,不要重复哦!');

            }

        },'json');

    })

3. [代码]页面    

?

1

<a class="z" id="<{$list.id}>" href="javascript:void(0);">赞<span><{$list.zan}></span></a>

 

 

转载于  https://www.oschina.net/code/snippet_2401565_49607

posted @ 2019-01-14 09:55  孙中明  阅读(202)  评论(0编辑  收藏  举报