Home > Backend Development > PHP Tutorial > php+xml结合Ajax实现点赞功能完整实例_PHP

php+xml结合Ajax实现点赞功能完整实例_PHP

WBOY
Release: 2016-05-31 13:17:43
Original
905 people have browsed it

本文实例讲述了php+xml结合Ajax实现点赞功能的方法。分享给大家供大家参考。具体如下:

使用xml、php和Ajax实现点赞功能,不需要链接数据库,使用php来修改xml的内容,使用Ajax直接或许xml的内容。

一、准备好xml:

<&#63;xml version="1.0"&#63;> 
<goodtree> 
  <goodnode> 
    <id>0</id> 
    <count>17</count> 
  </goodnode> 
  <goodnode> 
    <id>1</id> 
    <count>37</count> 
  </goodnode> 
  <goodnode> 
    <id>2</id> 
    <count>67</count> 
  </goodnode>   
</goodtree>

Copy after login

其中ID只是用来看清楚排序的,没有实际的调用作用。

二、准备好HTML

<div id="goodcount">
  <span>0</span><button onclick="goodplus(0);">good+1</button>
  <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>
</div>

Copy after login

三、JAVASCRIPT 包括Ajax在内,还添加了判断cookie的功能

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).innerHTML); 
    if(checkcookie(gindex) == true){ 
      num = num + 1; 
      senddata(gindex); 
    }else{ 
      alert("你已经点过赞咯!")   
    } 
} 

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){ 
        if(flag == 0){ 
          xmldoc = xmlhttp.responseXML; 
          var count = xmldoc.getElementsByTagName('count'); 
          var span2 = document.getElementsByTagName('span'); 
          for(var j = 0; j < count.length; j++){ 
            span2.item(j).innerHTML = count[j].childNodes[0].nodeValue; 
          } 
        }else if(flag == 1){ 
          xmldoc2 = xmlhttp.responseText; 
          var span3 = document.getElementsByTagName('span'); 
          span3.item(aindex).innerHTML = xmldoc2; 
        } 
      } 
    } 
    if(flag == 0){ 
      xmlhttp.open("GET","/ajax/foodmap/index.xml"); 
    }else{ 
      xmlhttp.open("GET","/ajax/foodmap/index.php&#63;num=" + num + "&aindex=" + aindex,true);   
    } 
    xmlhttp.send(); 
} 

//判断是否已经存在了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,参数是名称。 
    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的值存在名称和等号的后面,所以内容的开始位置应该是加上长度和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 ""; 
} 

//设置cookie 
function setCookie(c_name,value,expiredays){
//存入名称,值,有效期。有效期到期事件是今天+有效天数。然后存储cookie, 
    var exdate=new Date(); 
    exdate.setDate( exdate.getDate() + expiredays ) 
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) &#63; "" : "; expires=" + exdate.toGMTString()) 
} 

Copy after login

四、通过php来修改xml的数据,一开始调用xml的数据的时候不需要php文件。

<&#63;php 
$num = $_GET['num']; 
echo $_GET['num']; 
$aindex = $_GET['aindex']; 
$dom=new DOMDocument('1.0'); 
$dom->load('index.xml'); 
$goodnode=$dom->getElementsByTagName('goodnode'); 
$goodnode = $goodnode->item($aindex); 
$items = $goodnode->getElementsByTagName('count'); 
foreach($items as $a){ 
  $a->nodeValue = $_GET['num']; 
} 
$dom->save('index.xml'); 
&#63;>
Copy after login

完成。

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template