MENU
Synchronous Transfer POST
Unlike form data, AJAX data is not automatically URL-encoded (unless you use FormData). However, the AJAX data is automatically URL-decoded at the PHP end, so that you can use $_GET, $_POST, and $_REQUEST directly.A very detailed synopsis of XMLHttpRequest can be found at:
https://developer.mozilla.org/en-US/ docs/Web/API/XMLHttpRequest
This example demonstrates the use of the "POST" method in carrying out an online poll. Because the asynchronous parameter in xhr.open() is set to false, there is no need to define the callback function using xhr.onreadystatechange.
RESETRUNFULL
RESETRUNFULL
<?php
$v = $_POST['vote'];
$content=file("poll.txt");
list($yes,$no)=explode("|",$content[0]);
if ($v==0) $yes++;
if ($v==1) $no++;
file_put_contents("poll.txt","$yes|$no");
?>
<h3>Yes: <?php echo $yes; ?> </h3><br />
<h3>No: <?php echo $no; ?> </h3>
<!DOCTYPE html><html><head>
<script>
function vote(v){
var xhr;
xhr=new XMLHttpRequest();
xhr.open("POST","poll.php",false);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send("vote="+v);
document.getElementById("poll").innerHTML=xhr.responseText;
}
</script></head>
<body>
<div id="poll">
<h3>Do you think AJAX is fun?</h3>
Yes: <input type="radio" name="vote" value="0" onclick="vote(this.value)"/>
No: <input type="radio" name="vote" value="1" onclick="vote(this.value)"/>
</div>
</body></html>