This article mainly introduces how to use php + xml and Ajax to implement the like feature. it analyzes in detail XML files, html pages, and Ajax's tips for implementing the like feature with php in the form of a complete example, it is very practical. if you need it, you can refer to the following example to describe how to use php + xml and Ajax to implement the like function. 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.