php教程 ajax XMLHttpRequest POST實例代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.bkjia.com/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<head>
<script type="text/javascript教程">
function showUser(str){
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("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","ajax.php",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8;");
xmlhttp.send('q='+str);
}
</script>
</head>
<body>
<form method="post">
ajax的POST<input name="submit" type="submit" onclick="showUser(28);return false;" value="查詢"/>
</form>
<form action="ajax.php" method="post">
真正的POST<input name="q" type="submit" value="28"/>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
php 代碼<?php
header('Content-Type:text/html;charset=utf-8;');
echo "POST數據: ".implode('',file('php://input'));
echo "<br>POST[q] ".$_POST['q']."<br>";
print_r($_POST);
?>