Php+xml combined with Ajax to achieve a complete example of point-praise function _php Skills

Source: Internet
Author: User
Tags set cookie setcookie

The example of this article describes the method of Php+xml combining Ajax to realize the point-praise function. Share to everyone for your reference. Specifically as follows:

Use XML, PHP, and Ajax to implement the point-and-click functionality without the need to link the database, use PHP to modify the content of the XML, and use Ajax to directly or possibly XML content.

One, ready xml:

<?xml version= "1.0"?> 
<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>

Where the ID is only used to see the clear sort, there is no actual invocation effect.

Second, prepare the 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>

Third, JAVASCRIPT includes Ajax, but also added the ability to judge 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 ("You've been lit!") 
    ')} 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?num=" + num + "&aindex=" + aindex,true); 
} xmlhttp.send (); 
    //Determine if there is already a 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; 
    }//Get Cookie function GetCookie (c_name) {//Get cookie, parameter is 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 to find, find and then determine the end of the bitPlacing C_start = C_start + c_name.length + 1; 
        The value of the cookie exists after the name and the equal sign, so the start position of the content should be plus 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)); 
      Returns the content, decodes. 
} return ""; //Set Cookie function Setcookie (c_name,value,expiredays) {//Deposit name, value, validity period. Expiration event is today + valid days. 
    Then store cookies, 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, a start to call the XML data when the PHP file does not need.

<?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 will help you with your PHP program design.

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.