Php + mysql combined with Ajax to implement a complete example of the like function, mysqlajax. Php + mysql: a complete example of thumb ups with Ajax. the example in mysqlajax describes how to use php + mysql with Ajax to implement the thumb ups function. Share it with you for your reference. The details are as follows: php + mysql combined with Ajax to implement a complete example of the like function, mysqlajax
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 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