php+mysql結(jié)合Ajax實(shí)現(xiàn)點(diǎn)贊功能完整實(shí)例
文章主要介紹了php+mysql結(jié)合Ajax實(shí)現(xiàn)點(diǎn)贊功能,以一個(gè)完整實(shí)例形式詳細(xì)分析了實(shí)現(xiàn)點(diǎn)贊功能中涉及的html頁面、Ajax功能及php方法的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
要實(shí)現(xiàn)點(diǎn)贊功能,有多種實(shí)現(xiàn)方式,這里總結(jié)一下利用Ajax,php和mysql來實(shí)現(xiàn)點(diǎn)贊的數(shù)據(jù)的功能。具體步驟如下:
一、頁面中的HTML代碼部分:
?
1
2
3
4
5
6
7
8
9
10
11
<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>
二、寫javascript
1、實(shí)現(xiàn)上面的button的點(diǎn)擊事件goodplus
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var span = document.getElementsByTagName('span');/pic/p>
var num; /pic/p>
var flag = 0; /pic/p>
function goodplus(gindex){
flag = 1;
num = parseInt(span.item(gindex-1).innerHTML);
if(checkcookie(gindex) == true){
num = num + 1;
senddata(gindex); /pic/p>
}else{
alert("你已經(jīng)點(diǎn)過贊咯!")
}
}
2、頁面一打開時(shí)就應(yīng)該更新點(diǎn)贊數(shù)據(jù)
?
1
2
3
for(var i = 1; i < span.length + 1; i++){
senddata(i);
}
3、通過Ajax獲取數(shù)據(jù)senddata函數(shù)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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; /pic/p>
var cookieindex = aindex - 1;
document.getElementsByTagName('span').item(cookieindex).innerHTML = txt; /pic/p>
}
}
xmlhttp.open("GET","路徑/index.php?num=" + num + '&flag=' + flag + '&aindex=' + aindex,true);
xmlhttp.send();
}
4、通過設(shè)置cookie來判斷是否已經(jīng)點(diǎn)贊,如果有cookie則提示已經(jīng)點(diǎn)贊,如果沒有cookie則允許點(diǎn)贊,而且會(huì)設(shè)置cookie
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/pic/p>
function checkcookie(gindex){
var thiscookie = 'goodplus' + gindex;
var mapcookie = getCookie(thiscookie)
if (mapcookie!=null && mapcookie!=""){
return false;
}else {
setCookie(thiscookie,thiscookie,365);
return true;
}
}
/pic/p>
function getCookie(c_name){/pic/p>
if (document.cookie.length > 0){/pic/p>
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1){ /pic/p>
c_start = c_start + c_name.length + 1 ;
/pic/p>
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));/pic/p>
}
}
return "";
}
/pic/p>
function setCookie(c_name,value,expiredays){
/pic/p>
var exdate=new Date();
exdate.setDate( exdate.getDate() + expiredays )
document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : "; expires=" + exdate.toGMTString())
}
三、index.php頁面:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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)
?>
四、最終的index.html頁面如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html PUBLIC "-/pic/pic/pic/pic/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="/pic/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標(biāo)題文檔</title>
</head>
<body>
<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>
<script type="text/javascript">
var span = document.getElementsByTagName('span');
var num;
var flag = 0;
for(var i = 1; i < span.length + 1; i++){
senddata(i);
}
function goodplus(gindex){
flag = 1;
num = parseInt(span.item(gindex-1).innerHTML);
if(checkcookie(gindex) == true){
num = num + 1;
senddata(gindex);
}else{
alert("你已經(jīng)點(diǎn)過贊咯!")
}
}
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;
var cookieindex = aindex - 1;
document.getElementsByTagName('span').item(cookieindex).innerHTML = txt;
}
}
xmlhttp.open("GET","/ajax/json/index.php?num=" + num + '&flag=' + flag + '&aindex=' + aindex,true);
xmlhttp.send();
}
/pic/p>
function checkcookie(gindex){
var thiscookie = 'sdcity_foodmap_goodplus' + gindex;
var mapcookie = getCookie(thiscookie)
if (mapcookie!=null && mapcookie!=""){
return false;
}else {
setCookie(thiscookie,thiscookie,365);
return true;
}
}
/pic/p>
function getCookie(c_name){
/pic/p>
if (document.cookie.length > 0){
/pic/p>
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1){
/pic/p>
c_start = c_start + c_name.length + 1 ;
/pic/p>
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));/pic/p>
}
}
return "";
}
/pic/p>
function setCookie(c_name,value,expiredays){
/pic/p>
var exdate=new Date();
exdate.setDate( exdate.getDate() + expiredays )
document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : "; expires=" + exdate.toGMTString())
}
</script>
</body>
</html>
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
【php+mysql結(jié)合Ajax實(shí)現(xiàn)點(diǎn)贊功能完整實(shí)例】相關(guān)文章:
php+mysql實(shí)現(xiàn)無限分類實(shí)例詳解09-11
php中實(shí)現(xiàn)回刪功能實(shí)例01-26
ThinkPHP實(shí)現(xiàn)支付寶接口功能實(shí)例08-27
php+mysql查詢優(yōu)化簡單實(shí)例08-07
PHP中檢測ajax請(qǐng)求的代碼實(shí)例03-16
Java實(shí)現(xiàn)多繼承的實(shí)例08-26
基于PHP+Ajax實(shí)現(xiàn)表單驗(yàn)證的詳解08-06
php實(shí)現(xiàn)偽靜態(tài)的方法實(shí)例11-30
c#實(shí)現(xiàn)sunday算法實(shí)例10-12
- 相關(guān)推薦