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.jzread.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);
?>