Php + mysql combined with Ajax to implement a complete example of the like feature

Source: Internet
Author: User
Tags set cookie
This article mainly introduces how to use php + mysql to implement the like feature with Ajax, and analyzes in detail the html pages and Ajax functions involved in implementing the like feature in the form of a complete instance, as well as the php method usage skills, it is very practical. if you need it, you can refer to the example in this article to describe how to use php + mysql with Ajax to implement the like feature. Share it with you for your reference. The details are as follows:

There are multiple implementation methods to implement the like feature. here we will summarize the use of Ajax, php, and mysql to implement the like data feature. The procedure is as follows:

I. HTML code section on the page:

0good+10good+10good+10good+1

II. write javascript

1. implement the above button click event goodplus

Var span = document. getElementsByTagName ('span '); // Obtain the dom var num for storing the number of likes; // The number of likes var flag = 0; // Mark function goodplus (gindex) in different situations) {flag = 1; num = parseInt (span. item (gindex-1 ). innerHTML); if (checkcookie (gindex) = true) {num = num + 1; senddata (gindex ); // modify the data on the page through Ajax} else {alert ("you have liked it! ")}}

2. when the page is opened, the like data should be updated.

for(var i = 1; i < span.length + 1; i++){   senddata(i);  }

3. use Ajax to obtain the senddata function

Function senddata (aindex) {var xmlhttp; var txt; if (window. XMLHttpRequest) {xmlhttp = new XMLHttpRequest ();} else {xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");} xmlhttp. onreadystatechange = function () {if (xmlhttp. readyState = 4 & xmlhttp. status = 200) {txt = xmlhttp. responseText; // Get the returned data var cookieindex = aindex-1; document. getElementsByTagName ('span '). item (cookieindex ). innerHTML = txt; // value assignment } Xmlhttp. open ("GET", "path/index. php? Num = "+ num + '& flag =' + flag + '& aindex =' + aindex, true); xmlhttp. send ();}

4. you can set the cookie to determine whether the like has been liked. if a cookie exists, the system prompts that the like has been liked. If no cookie exists, the system allows the like Operation and sets the cookie.

// Determine whether the cookie function checkcookie (gindex) {var thiscookie = 'goodplus '+ gindex; var mapcookie = getCookie (thiscookie) if (mapcookie! = Null & mapcookie! = "") {Return false;} else {setCookie (thiscookie, thiscookie, 365); return true ;}// get cookie function getCookie (c_name) {// get cookie, the parameter is the name. If (document. cookie. length> 0) {// when the cookie is not empty, search for the name c_start = document. cookie. indexOf (c_name + "="); if (c_start! =-1) {// if the start position is not-1, it is found. after it is found, determine the end position c_start = c_start + c_name.length + 1; // The cookie value is followed by the name and equal sign, so the start position of the content should be added with the length and 1 c_end = document. cookie. indexOf (";", c_start); if (c_end =-1) {c_end = document. cookie. length;} return unescape (document. cookie. substring (c_start, c_end); // return the content and decode it.} Return "";} // Set cookie function setCookie (c_name, value, expiredays) {// store name, value, and validity period. The expiration date is today + valid days. Then store the cookie, var exdate = new Date (); exdate. setDate (exdate. getDate () + expiredays) document. cookie = c_name + "=" + escape (value) + (expiredays = null )? "": "; Expires =" + exdate. toGMTString ())}

III. index. php page:

<?php$num = $_GET['num'];$aindex = $_GET['aindex'];$con = mysql_connect("localhost","root","");if (!$con){ die('Could not connect: ' . mysql_error());}mysql_select_db("goodplus", $con);$sql0s = "SELECT * FROM `good` where `id` = ".$aindex;$sql0 = mysql_query($sql0s);if($_GET['flag'] == 0){ while($row = mysql_fetch_array($sql0)){ echo $row['value']; }}else if($_GET['flag'] == 1){ $sql="UPDATE `goodplus`.`good` SET `value` = '".$num."' WHERE `good`.`id` = ".$aindex;  if (!mysql_query($sql,$con)){ die('Error: ' . mysql_error()); } echo $num;}mysql_close($con)?>

The final index.html page is as follows:

 Untitled Document0Good + 10Good + 10Good + 10Good + 1

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.