Home php教程 PHP源码 thinkphp+ajax局部刷新点赞

thinkphp+ajax局部刷新点赞

May 23, 2016 pm 05:09 PM

thinkphp点赞方法

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();
        }
         
    }
Copy after login

js

$(".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'); 
    })
Copy after login

页面

<a class="z" id="<{$list.id}>" href="javascript:void(0);">赞<span><{$list.zan}></span></a>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24