本文實例講述了php+mysql結合Ajax實現(xiàn)點贊功能的方法。分享給大家供大家參考。具體如下:
要實現(xiàn)點贊功能,有多種實現(xiàn)方式,這里總結一下利用Ajax,php和mysql來實現(xià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、實現(xiàn)上面的button的點擊事件goodplus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
var span = document.getElementsByTagName( 'span' ); //獲取存放點贊數(shù)的dom var num; //點贊數(shù) var flag = 0; //不同情況的標記 function goodplus(gindex){ flag = 1; num = parseInt(span.item(gindex-1).innerHTML); if (checkcookie(gindex) == true ){ num = num + 1; senddata(gindex); //通過Ajax修改頁面上的數(shù)據(jù) } else { alert( "你已經(jīng)點過贊咯!" ) } } |
2、頁面一打開時就應該更新點贊數(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; //獲取返回的數(shù)據(jù) var cookieindex = aindex - 1; document.getElementsByTagName( 'span' ).item(cookieindex).innerHTML = txt; //賦值 } } xmlhttp.open( "GET" , "路徑/index.php?num=" + num + '&flag=' + flag + '&aindex=' + aindex, true ); xmlhttp.send(); } |
4、通過設置cookie來判斷是否已經(jīng)點贊,如果有cookie則提示已經(jīng)點贊,如果沒有cookie則允許點贊,而且會設置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
|
//判斷是否已經(jīng)存在了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 ; } } //獲取cookie function getCookie(c_name){ //獲取cookie,參數(shù)是名稱。 if (document.cookie.length > 0){ //當cookie不為空的時候就開始查找名稱 c_start = document.cookie.indexOf(c_name + "=" ); if (c_start != -1){ //如果開始的位置不為-1就是找到了、找到了之后就要確定結束的位置 c_start = c_start + c_name.length + 1 ; //cookie的值存在名稱和等號的后面,所以內(nèi)容的開始位置應該是加上長度和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)); //返回內(nèi)容,解碼。 } } return "" ; } //設置cookie function setCookie(c_name,value,expiredays){ //存入名稱,值,有效期。有效期到期事件是今天+有效天數(shù)。然后存儲cookie, 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 "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns = "http://www.w3.org/1999/xhtml" > < head > < meta http-equiv = "Content-Type" content = "text/html; charset=gb2312" /> < title >無標題文檔</ 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)點過贊咯!") } } 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(); } //判斷是否已經(jīng)存在了cookie 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; } } //獲取cookie function getCookie(c_name){ //獲取cookie,參數(shù)是名稱。 if (document.cookie.length > 0){ //當cookie不為空的時候就開始查找名稱 c_start = document.cookie.indexOf(c_name + "="); if (c_start != -1){ //如果開始的位置不為-1就是找到了、找到了之后就要確定結束的位置 c_start = c_start + c_name.length + 1 ; //cookie的值存在名稱和等號的后面,所以內(nèi)容的開始位置應該是加上長度和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));//返回內(nèi)容,解碼。 } } return ""; } //設置cookie function setCookie(c_name,value,expiredays){ //存入名稱,值,有效期。有效期到期事件是今天+有效天數(shù)。然后存儲cookie, var exdate=new Date(); exdate.setDate( exdate.getDate() + expiredays ) document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : "; expires=" + exdate.toGMTString()) } </ script > </ body > </ html > |
希望本文所述對大家的php程序設計有所幫助。