1

我想用 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');

?>
4

1 回答 1

1

get方法将参数作为 URL 中的查询字符串发送,而post查询字符串在 http 标头中发送:

xmlhttp.open("POST","/project3/php/update.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("random="+x[x.length-1].innerHTML);

在 PHP 方面,posts 变量被添加到全局关联数组中,如下所示:

<?php echo $_POST['random'];
于 2013-03-17T14:17:20.630 回答