Php+xml combined with Ajax for a complete example of how to like a feature, xmlajax_php tutorial

Source: Internet
Author: User

Php+xml combined with Ajax to achieve a full example of the function, Xmlajax


In this paper, we describe the method of Php+xml combining Ajax to realize the function of liking. Share to everyone for your reference. Specific as follows:

Using XML, PHP and Ajax to implement the like feature, do not need to link the database, using PHP to modify the content of XML, using AJAX directly or XML content.

First, prepare the XML:

<?xml version= "1.0"?> 
 
     
  
        
   
    
     
    0
   
         
   
    
   
       
  
      
  
        
   
    
     
    1
   
         
   
    
     
    pns
   
       
  
      
  
        
   
    
     
    2
   
       
  
    
     
    the
   
     
      
 
  

Where the ID is only used to see the sort clearly, no actual call function.

B. Prepare HTML

  0 good+1  0 good+1  0 good+1  0 good+1

Third, JAVASCRIPT including Ajax, but also added the ability to determine the 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 ("You've already ordered praise!")     ")}} 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 = =) {I           F (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?num=" + num + "&aindex=" + aindex,true); } xmlhttp.send ();     }//To determine if a cookie function Checkcookie (gindex) {var Thiscookie = ' Sdcity_foodmap_goodplus ' + gindex is already present;     var Mapcookie = GetCookie (Thiscookie) if (mapcookie!=null && mapcookie!= "") {return false;       }else {Setcookie (thiscookie,thiscookie,365);     return true;     }}//Gets the cookie function GetCookie (c_name) {//Gets the cookie, the parameter is the 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, the location is determined to determine the end position C_start = C_start + c_name.length + 1; The value of//cookie exists after the name and the equal sign, theWith the starting position of the content should be plus the 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));//return content, decode. }} return "";} Set cookie function Setcookie (c_name,value,expiredays) {//Deposit name, value, expiration date. The expiration event is today + valid days.     Then store the cookie, var exdate=new Date ();  Exdate.setdate (exdate.getdate () + expiredays) Document.cookie = c_name + "=" + Escape (value) + ((expiredays==null)? "" : ";  Expires= "+ exdate.togmtstring ())}

Four, through PHP to modify the XML data, the beginning of the invocation of XML data when the PHP file is not required.

<?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 ');?>

Complete.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/950896.html www.bkjia.com true http://www.bkjia.com/PHPjc/950896.html techarticle Php+xml combined with Ajax to achieve a full example of the function, Xmlajax The example of this article describes the Php+xml with Ajax to implement the method of liking the function. Share to everyone for your reference. As follows: Use ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.