Article or point of view and so on like the function implementation (thinkphp)

Source: Internet
Author: User

Front-End Code:

class='btm'class='zan' id= ' {$article. ID} " href="javascript:void (0); "> Likes (<span>{$article .likescount}</span>) </a></div>

Of course, the processing of data with Ajax, but first to introduce jquery and Jquery.cookie.js, this is Baidu download it yourself. The front end uses cookies to limit the effect of repeating the likes.

JS section:

$(". Zan"). Live ('Click', function () {varoa=$ ( This); varId=oa.attr ('ID');//Get ID Property        varVl=oa.find ("span"). text (); VL=parseint (VL) +1; if(!$.cookie (ID)) {            //Console.log (' no cache ');$.post ("{: U ("Article/zan")}", {id:id},function (data) {Console.log (data)if(data.status=='OK') {alert ('+1 likes');//simulate asynchronous data plus 1$.cookie (Id,id);//change the flag initial value to ensure that the function executes only onceOa.find ("span"). Text (VL);//page element plus 1                }            },'JSON'); }Else{            //Console.log (' with cache ');Alert'you've already ordered it!'); }        return false; })

Back end receive data: IndexController.class.php

 Public function Zan () {      $data [' id ']=isset ($_post[' id '))? Intval (Trim ($_post[' id ')):0;      $db=M (' article ');       = $db,where($data),setinc (' Zan ');       if ($res) {        $this->ajaxreturn ($data, ' OK ',1);        Exit ();      } Else {        $this->ajaxreturn ($data, ' fail ',0);        Exit ();      }    }

Jquery.cookie.js:

/*! * JQuery Cookie Plugin v1.4.1 *Https://github.com/carhartl/jquery-cookie* Copyright 2006, Klaus Hartl * released under the MIT license*/(function (factory) {if(typeofdefine = = ='function'&&define.amd) {//AMD (Register as an anonymous module)Define (['jquery'], factory); } Else if(typeofExports = = ='Object') {        //Node/commonjsModule.exports = Factory (Require ('jquery')); } Else {        //Browser GlobalsFactory (JQuery); }} (function ($) {varpluses =/\+/G; function encode (s) {returnConfig.raw?S:encodeuricomponent (s); } function decode (s) {returnConfig.raw?S:decodeuricomponent (s); } function Stringifycookievalue (value) {returnEncode (Config.json?json.stringify (value): String (value)); } function Parsecookievalue (s) {if(S.indexof ('"') ===0) {            //This was a quoted cookie as according to RFC2068, unescape ...s = S.slice (1, -1). Replace (/\\"/g, '"'). Replace (/\\\\/g,'\\');        }        Try {            //Replace server-side written pluses with spaces. //If We can ' t decode the cookie, ignore it, it ' s unusable. //If We can ' t parse the cookie, ignore it, it ' s unusable.s = decodeuricomponent (S.replace (pluses,' ')); returnConfig.json?Json.parse (s): s; } Catch(e) {}} function read (s, converter) {varValue = Config.raw?S:parsecookievalue (s); return$.isfunction (Converter)?Converter (value): value; }    varConfig = $.cookie =function (key, value, options) {//Write        if(Arguments.length >1&&!$.isfunction (value)) {Options=$.extend ({}, Config.defaults, options); if(typeofOptions.expires = = =' Number') {                vardays = options.expires, t = Options.expires =NewDate (); T.setmilliseconds (T.getmilliseconds ()+ Days * 864e+5); }            return(Document.cookie =[Encode (key),'=', Stringifycookievalue (value), Options.expires?'; expires='+ options.expires.toUTCString ():"',//Use expires attribute, Max-age isn't supported by IEOptions.path?'; path='+ Options.path:"', Options.domain?'; domain='+ Options.domain:"', Options.secure?'; secure':"'].join ("')); }        //Read        varresult = key?undefined: {},//To prevent the as loop in the first place assign an empty array//In case There is no cookies at all. Also prevents odd result when//calling $.cookie ().cookies = Document.cookie? Document.cookie.split ('; '): [], I=0, L=cookies.length;  for(; i < L; i++) {            varParts = Cookies[i].split ('='), name=Decode (Parts.shift ()), Cookie= Parts.join ('='); if(Key = = =name) {                //If second argument (value) is a function it ' s a converter ...result =Read (cookie, value);  Break; }            //Prevent Storing a cookie that we couldn ' t decode.            if(!key && (cookie = read (cookie))!==undefined) {Result[name]=cookies; }        }        returnresult;    }; Config.defaults= {}; $.removecookie=function (key, options) {//must not alter options, thus extending a fresh object ...$.cookie (Key,"', $.extend ({}, Options, {expires:-1 })); return!$.cookie (key); };}));

.

Article or point of view and so on like the function implementation (thinkphp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.