php+xml结合Ajax实现点赞功能完整实例_PHP
本文实例讲述了php+xml结合Ajax实现点赞功能的方法。分享给大家供大家参考。具体如下:
使用xml、php和Ajax实现点赞功能,不需要链接数据库,使用php来修改xml的内容,使用Ajax直接或许xml的内容。
一、准备好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>
其中ID只是用来看清楚排序的,没有实际的调用作用。
二、准备好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>
三、JAVASCRIPT 包括Ajax在内,还添加了判断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("你已经点过赞咯!") } } 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(); } //判断是否已经存在了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; } } //获取cookie function getCookie(c_name){ //获取cookie,参数是名称。 if (document.cookie.length > 0){ //当cookie不为空的时候就开始查找名称 c_start = document.cookie.indexOf(c_name + "="); if (c_start != -1){ //如果开始的位置不为-1就是找到了、找到了之后就要确定结束的位置 c_start = c_start + c_name.length + 1 ; //cookie的值存在名称和等号的后面,所以内容的开始位置应该是加上长度和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 ""; } //设置cookie function setCookie(c_name,value,expiredays){ //存入名称,值,有效期。有效期到期事件是今天+有效天数。然后存储cookie, var exdate=new Date(); exdate.setDate( exdate.getDate() + expiredays ) document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : "; expires=" + exdate.toGMTString()) }
四、通过php来修改xml的数据,一开始调用xml的数据的时候不需要php文件。
<?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'); ?>
完成。
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
