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

Source: Internet
Author: User
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, very practical value

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, very practical value

This example describes 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} xmlhttp. op En ("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 1c_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 the 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.