Home  >  Article  >  Backend Development  >  A complete example of using php+xml combined with Ajax to realize the like function, xmlajax_PHP tutorial

A complete example of using php+xml combined with Ajax to realize the like function, xmlajax_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:08:26802browse

php+xml combined with Ajax to implement a complete example of the like function, xmlajax

The example in this article describes the method of php+xml combined with Ajax to implement the like function. Share it with everyone for your reference. The details are as follows:

Use xml, php and Ajax to implement the like function without linking to the database. Use php to modify the content of xml, and use Ajax to directly change the content of xml.

1. Prepare 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>

The ID is only used to see the sorting, and has no actual calling effect.

2. Prepare 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>

3. JAVASCRIPT, including Ajax, also adds the function of judging cookies

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()) 
} 

4. Modify xml data through php. No php file is required when calling xml data at the beginning.

<&#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;>

Done.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/950896.htmlTechArticlephp+xml combined with Ajax to achieve a complete example of the like function, xmlajax This example describes the implementation of php+xml combined with Ajax Like function method. Share it with everyone for your reference. The details are as follows: Use...
Statement:
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