我想用 http 请求和 php 向 xml 文件发送一个随机数。但我真的不知道如何添加生成的数字的值并将其添加到帖子中。
这就是我到目前为止所拥有的。
var x=document.getElementsByClassName("demo");
x[x.length-1].innerHTML=Math.floor((Math.random()*1000000)+1);
// Generates a random number and print it on the last demo class
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/project3/php/update.php",true); //Calls the php update file
xmlhttp.send();
PHP 文件
<?php
$dom = new DOMDocument();
$dom->load('../stickers.xml');
$stickers = $dom->documentElement;
$xpath = new DOMXPath($dom);
$result = $xpath->query('/stickers/sticker[id="$POST"]/id'); //Not sure.
$result->item(0)->nodeValue .= 'hi';
echo $dom->saveXML();
$dom->save('../stickers.xml');
?>