jquery+php to achieve the likes, stepping function

Source: Internet
Author: User

To achieve the likes, stepping on the function needs some requirements:

1. Record the user's IP, determine whether the user's voting behavior is valid according to IP

2. Need two tables, one is to count the number of like and tread, one is a record like or stepping on the user's IP

3. Page loading complete need to display the like and stepping of the data with Ajax from the server to obtain, after each point like, point stepping is also used Ajax from the server to obtain

HTML code (amount, because this is the main implementation of logic, so the interface is very simple, hehe)

This HTML code is very simple, not much to say

<body><p ><input type= "button" value= "likes" class= "Evaluate" id= "Zan" > <label for= "like" > Support number: </label><span id= "like" > 0</span> <label for= "like_percent" > Support rate: </label> <span id = " Like_percent ">0</span><strong>%</strong></p><p ><input type=" button "value=" step "class=" Evaluate "id=" Cai "> <label for=" Unlike "> Support number: </label><span id=" Unlike "> 0</span> <label for= "unlike_percent" > Support rate: </label> <span id = "Unlike_percent" >0</span><strong> %</strong></p><p id = "MSG" ></P></body>

jquery Code

Because the AJAX code of the button event is almost the same either for initialization or later, so it's written in a function

Parameters $id is the article ID (here, for example, for example, you have 10 articles, each article has like and stepping function, you can give each article an ID, and then to the service end of this article is the bottom of the like and step number)

When the vote fails, the failed message obtained from the server is displayed on the page with the faded animation effect.

function GetData (URL, $id) {     $.getjson (url,{id: $id},function (data) {         if (data.success==1) {//Poll succeeded $ (' #like '). HTML (data.like); $ (' #like_percent '). HTML (data.like_percent); $ (' #unlike '). HTML (data.unlike); $ (' #unlike_percent ') . html (data.unlike_percent);         } ELSE{//Poll failed $ ("#msg"). HTML (DATA.MSG). Show (). CSS ({' Opacity ': 1, ' top ': ' 40px '}). Animate ({top: ' -50px ', opacity:0}, " Slow ");        }     
The remaining jquery execution code

$ (document). Ready (function () {//) set the article ID to 1$id = 1;var url = "operator.php" for testing,//Initialize GetData (URL, $id); $ ('. Evaluate ') ). Click (function () {if ($ (). is (' #zan ')) {url = url + "? Action=like";} else if ($ (this). is (' #cai ')) {url = url + "? Actiion=unlike";} GetData (URL, $id);});

Then there are two tables needed, the votes table is used to record the number of user votes corresponding to the article or comment, and we default to write a data ID 1 to demonstrate that the VOTES_IP table is used to record the IP of each poll, and the program determines whether the vote is valid based on the user's IP. The ID field in VOTES_IP records the article ID

CREATE TABLE IF not EXISTS ' votes ' (   ' ID ' int.) NOT NULL auto_increment,   ' likes ' int (ten) not null DEFAULT ' 0 ', 
    ' unlikes ' int (ten) not NULL default ' 0 ',   PRIMARY KEY (' id ')) engine=myisam  default Charset=utf8;   INSERT into ' votes ' (' id ', ' likes ', ' Unlikes ') VALUES (1, ten);  CREATE TABLE IF not EXISTS ' votes_ip ' (   ' id ' int (ten) not NULL,   ' IP ' varchar (+) NOT null) Engine=myisam DEFAULT C Harset=utf8;
PHP code

(the part of the code connected to the database, I did not post it)

$evaluate = $_get[' action ']; $id = $_get[' id '); $ip = Get_client_ip (); if ($evaluate = = ' like ') {likes (1, $id, $ip, $db);} else if ($evaluate = = ' unlike ') {likes (0, $id, $ip, $db);} Else{echo jsons ($id, $db);}

Functions required by PHP

function likes ($type, $id, $ip, $db) {$ip _sql=mysqli_query ($db, "Select IP from votes_ip where $id = ' $id ' and ip= ' $ip '");     $count =mysqli_num_rows ($ip _sql);     if (count==0) {        if ($type ==1) {            $sql = "update votes set likes=likes+1 where id=". $id;         } else{            $sql = "update votes set unlikes=unlikes+1 where id=". $id;         Mysqli_query ($db, $sql);                  $sql _in = "INSERT into VOTES_IP (ID,IP) VALUES (' $id ', ' $ip ')";         Mysqli_query ($db, $sql _in);                if (Mysqli_affected_rows ($db) >0) {             echo jsons ($id, $db);         } else{             $arr [' success '] = ' server has an exception, please try it later ';             $arr [' msg '] = ' failed ';             echo Json_encode ($arr);         }     } else{         $msg = $type ==1? ' You've already praised ': ' You've stepped on ';         $arr [' success '] = 0;         $arr [' msg '] = $msg;         echo Json_encode ($arr);     } }

function Jsons ($id, $db) {     $query = mysqli_query ($db, "select * from votes where id=". $id);     $row = Mysqli_fetch_array ($query);     $like = $row [' likes '];     $unlike = $row [' unlikes '];     $arr [' Success ']=1;     $arr [' like '] = $like;     $arr [' unlike '] = $unlike;     $like _percent = Round ($like/($like + $unlike), 3) *100;     $arr [' like_percent '] = $like _percent;     $arr [' unlike_percent '] = (100-$like _percent);          Return Json_encode ($arr); }

function Get_client_ip () {if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "Unknown")) $ ip = getenv ("Http_client_ip"), ElseIf (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("http_x_forwarded _for ")," Unknown ")) $ip = getenv (" Http_x_forwarded_for "), ElseIf (getenv (" REMOTE_ADDR ") && strcasecmp (getenv (" Remote_addr ")," Unknown ")) $ip = getenv (" REMOTE_ADDR "), ElseIf (Isset ($_server[' remote_addr ')) && $_server[' Remote_addr '] && strcasecmp ($_server[' remote_addr '), "unknown") $ip = $_server[' remote_addr '];else$ip = " Unknown "; return ($IP);}
That's how it works.

jquery+php to achieve the likes, stepping function

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.