Php + xml combined with Ajax to implement a complete example of the like feature, xmlajax_PHP tutorial

Source: Internet
Author: User
Tags set cookie
Php + xml and Ajax are combined to implement a complete example of the like function, xmlajax. A complete example of php + xml and Ajax to implement the like feature is provided. the example in this document describes how php + xml and Ajax can be used to implement the like feature. Share it with you for your reference. The details are as follows: use php + xml combined with Ajax to implement a complete example of the like function, xmlajax

This example describes how to use php + xml and Ajax to implement the like feature. Share it with you for your reference. The details are as follows:

Use xml, php, and Ajax to implement the like feature. you do not need to connect to the database. use php to modify the xml content, and use Ajax to directly or xml content.

1. prepare xml:

<?xml version="1.0"?> 
    
       
   
    0
        
   
    17
      
     
       
   
    1
        
   
    37
      
     
       
   
    2
        
   
    67
      
     
 

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

2. prepare HTML

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

3. JAVASCRIPT, including Ajax, also adds the cookie judgment function

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 have already liked it! ")} 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 whether the cookie function checkcookie (gindex) {var thiscookie = 'sdcity _ foodmap_goodplus '+ gindex; var mapcookie = getCookie (thiscookie) already exists) if (mapcookie! = Null & mapcookie! = "") {Return false;} else {setCookie (thiscookie, thiscookie, 365); return true ;}// get cookie function getCookie (c_name) {// get cookie, the parameter is the name. If (document. cookie. length> 0) {// when the cookie is not empty, search for the name c_start = document. cookie. indexOf (c_name + "="); if (c_start! =-1) {// if the start position is not-1, it is found. after it is found, determine the end position c_start = c_start + c_name.length + 1; // The cookie value is followed by the name and equal sign, so the start position of the content should be added with 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 the content and decode it.} Return "";} // Set cookie function setCookie (c_name, value, expiredays) {// store name, value, and validity period. The expiration date 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 ())}

4. modify xml data using php. php files are not required when xml data is called at the beginning.

<?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 php programming.

Examples in this article describes how to use php + xml and Ajax to implement the like feature. Share it with you for your reference. The details are 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.