Php+mysql combined with Ajax for a complete example of how to like a feature, mysqlajax_php tutorial

Source: Internet
Author: User
Tags set cookie

Php+mysql combined with Ajax to achieve a full example of the function, Mysqlajax


In this paper, we describe the method of Php+mysql combining Ajax to realize the function of liking. Share to everyone for your reference. Specific as follows:

To achieve the likes of the function, there are many ways to achieve, here to summarize the use of ajax,php and MySQL to achieve the ability to like the data. The steps are as follows:

First, the HTML code part of the page:

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

Second, write JavaScript

1, achieve the above button click event Goodplus

var span = document.getElementsByTagName (' span ');//Gets the DOM Var num that holds the number of likes and/  or the number of likes  var flag = 0;//mark    of different cases function Goodplus (gindex) {   flag = 1;   num = parseint (Span.item (gindex-1). InnerHTML);   if (Checkcookie (gindex) = = true) {    num = num + 1;    SendData (Gindex); Modify the data on the page via Ajax   }else{    alert ("You've already ordered the praise!" ")    } }

2. When the page is open, you should update your liking data.

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

3. Get data SendData function via Ajax

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 = =) {     txt = Xmlhttp.responsetext; Gets the returned data     var cookieindex = aindex-1;     document.getElementsByTagName (' span '). Item (cookieindex). InnerHTML = txt; Assignment    }   }   xmlhttp.open ("GET", "path/index.php?num=" + num + ' &flag= ' + flag + ' &aindex= ' + Aindex, true);   

4, by setting a cookie to determine whether it has been liked, if there is a cookie hint has been liked, if no cookie is allowed to like, and set a cookie

Determine if there is already a 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;   }}//Gets the cookie function GetCookie (c_name) {//Gets the cookie, the parameter is the name.    if (Document.cookie.length > 0) {//When the cookie is not empty, start looking for the name C_start = document.cookie.indexOf (c_name + "="); if (C_start! =-1) {//If the starting position is not-1 is found, locate it and then determine the end position C_start = C_start + c_name.length + 1;//cookie Value exists name     And after the equals sign, so the starting position of the content should be plus 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 content, decode. }} return "";} Set cookie function Setcookie (c_name,value,expiredays) {//Deposit name, value, expiration date. The expiration event 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 ())}

Third, 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); $sql 0s = "select * from ' good ' where ' id ' = '. $aindex; $sql 0 = mysql_query ($sql 0s); if ($_g et[' flag '] = = 0) {while ($row = Mysql_fetch_array ($sql 0)) {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)?>

Four, the final index.html page is as follows:

 
  Untitled Document0 good+1 0 good+1 0 good+1 0 good+1

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/950898.html www.bkjia.com true http://www.bkjia.com/PHPjc/950898.html techarticle Php+mysql combined with Ajax to achieve a full example of the function, Mysqlajax The example of this article describes the Php+mysql with Ajax to implement the method of liking the function. Share to everyone for your reference. Specific as follows: ...

  • 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.