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

Source: Internet
Author: User
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 and requires

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 and requires

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 check the sorting, and has no actual calling effect.

2. Prepare HTML

0Good + 10Good + 10Good + 10Good + 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 1c_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 the 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.

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.