The example of this article describes the method of Php+mysql combining Ajax to realize the point-praise function. Share to everyone for your reference. Specifically as follows:
To achieve the point of praise function, there are a variety of ways to achieve, here summed up the use of ajax,php and MySQL to achieve the point of praise of the data function. The specific steps are as follows:
Part of the HTML code in the page:
<span>0</span>
<button onclick= "Goodplus (1);" >good+1</button>
<span>0</span>
<button onclick= "Goodplus (2);" >good+1</button>
<span>0</span>
<button onclick= "Goodplus (3);" >good+1</button>
<span>0</span>
<button onclick= "Goodplus (4);" >good+1</button>
Second, write JavaScript
1, to achieve the button above the Click event Goodplus
var span = document.getElementsByTagName (' span ');//Get the DOM Var num that holds the point of Praise
;//Dot count
var flag = 0;//Mark
of different circumstances 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 with Ajax
}else{
alert ("You've been lit!") ")
}
}
2. When the page is opened, you should update the point of praise 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);
Xmlhttp.send ();
}
4, by setting a cookie to determine whether it has been a point of praise, if there is a cookie tip has been point of praise, if no cookie is allowed to point praise, and will set cookies
To 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;
}//Get Cookie function GetCookie (c_name) {//Get cookie, parameter is 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, found and then determine the end of the position C_start = C_start + c_name.length + 1;
The value of the cookie exists after the name and the equal sign, so the start position of the content should be plus 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));//content, decoding.
} return ""; //Set Cookie function Setcookie (c_name,value,expiredays) {//Deposit name, value, validity period. Expiration event is today + valid days.
Then store cookies, 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 ($_get[' 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:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">