test_ajax_post.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <title>ajax_get方式</title> 5 <meta http-equiv="content-type" content="text/html;charset=utf-8" /> 6 <meta name="keywords" content="html,css,js,ajax" /> 7 <style type="text/css"> 8 9 </style> 10 <script type="text/javascript"> 11 function fun() 12 { 13 var name="xioming"; 14 var age="23"; 15 var infor="name="+name+"&age="+age; 16 var xmlhttp; 17 if (window.XMLHttpRequest) 18 {// code for IE7+, Firefox, Chrome, Opera, Safari 19 xmlhttp=new XMLHttpRequest(); 20 } 21 else 22 {// code for IE6, IE5 23 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 24 } 25 xmlhttp.onreadystatechange=function() 26 { 27 if (xmlhttp.readyState==4 && xmlhttp.status==200) 28 { 29 document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 30 } 31 } 32 xmlhttp.open("POST","test_ajax_post.php",true); 33 xmlhttp.setRequestHeader("content-type","application/x-www-form-urlencoded"); 34 xmlhttp.send(infor); 35 } 36 </script> 37 </head> 38 <body> 39 <h1>ajax_post提交方式</h1> 40 <div><input type="button" value="点我" onclick="fun()" /></div> 41 <div id="myDiv"></div> 42 </body> 43 </html>
test_ajax_post.php
1 <?php 2 header("Content-type:text/html;charset=utf8"); 3 $name=$_POST["name"]; 4 $age=$_POST["age"]; 5 //echo $name." ".$age." hello world"; 6 print_r($_POST); 7 ?>
显示结果: