Reputation: 2277
I want to send a random number to an xml-file with http request and php. But i cant really figure out how to add the value of the generated number and add it to the post.
This is what i have so far.
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 file
<?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');
?>
Upvotes: 1
Views: 746
Reputation: 10148
The get
method sends parameters as a query string in the URL, whereas the post
query string is sent within the http headers:
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);
On the PHP side of things, posts variables are added to a global associative array like so:
<?php echo $_POST['random'];
Upvotes: 1