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.111cn.net/1999/xhtml">
- <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
- <head>
- <script type="text/javascript">
- function showUser(str){
- if(window.XMLHttpRequest){
- xmlhttp=new XMLHttpRequest();
- }else{
- 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);
- ?>
|