js局部刷新【php技術】

 html代碼

  1. <html> 
  2.     <head> 
  3.         <meta http-equiv="content-type" content="text/html;charset=utf-8"> 
  4.         <title>js局部刷新</title> 
  5.         <style type="text/css"> 
  6.             #h1{ 
  7.                 text-align:center; 
  8.             } 
  9.             #div{ 
  10.                 margin:0 auto; 
  11.                 width:300px; 
  12.                 height:200px; 
  13.                 background:#ccc;     
  14.             } 
  15.             button{ 
  16.                 display:block; 
  17.                 margin:0 auto; 
  18.             } 
  19.         </style> 
  20.     </head> 
  21.     <body> 
  22.         <h1 id='h1'>js局部刷新</h1> 
  23.         <div id='div'> 
  24.          
  25.         </div> 
  26.         <h1 id='h1'><script>document.write(new Date())</script></h1> 
  27.         <button id="bid" name="button" onclick="fasong()">請求數據get方式</button> 
  28.         <button id="bid" name="button" onclick="fasong2()">請求數據post方式</button> 
  29.     </body> 
  30.     <script> 
  31.         //js異步請求php數據  (get方式) 
  32.         function fasong(){ 
  33.                 var xhr=new XMLHttpRequest();//生成XMLHttpRequest對象 
  34.                 xhr.onreadystatechange=function(){ 
  35.                     //當readyState==4 status==200表示響應成功 
  36.                 if(xhr.readyState==4 && xhr.status==200){ 
  37.                     //alert(xhr.responseText); 
  38.                 var data=xhr.responseText; 
  39.                 var divobj=document.getElementById('div') 
  40.                 divobj.innerHTML=data
  41.                 //hid.innerHTML=data
  42.                 }  
  43.                 } 
  44.                  
  45.                 //false 爲同步 
  46.                 xhr.open("get","index.php?",true);//準備好發送數據之前的準備 
  47.                 xhr.send();// 
  48.         } 
  49.          
  50.         //js異步請求php數據  (post方式) 
  51.         function fasong2(){ 
  52.                 var xhr=new XMLHttpRequest();//生成XMLHttpRequest對象 
  53.                 //alert(xhr); 
  54.                 /**/ 
  55.                 xhr.onreadystatechange=function(){ 
  56.                     //當readyState==4 status==200表示響應成功 
  57.                 if(xhr.readyState==4 && xhr.status==200){ 
  58.                     //alert(xhr.responseText); 
  59.                 var data=xhr.responseText; 
  60.                 var divobj=document.getElementById('div') 
  61.                 divobj.innerHTML=data
  62.                 //hid.innerHTML=data
  63.                 }  
  64.                 } 
  65.                  
  66.                 //xhr.open("get","index.php?"+str,true); 
  67.                 //false 爲同步 
  68.                 xhr.open("post","index.php?",true);//準備好發送數據之前的準備 
  69.                 xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");//模擬表單 否則無法顯示數據 
  70.                 xhr.send("suername=nihao&age=30");// 
  71.         } 
  72.          
  73.     </script> 
  74. </html> 

php代碼

 

  1. <?php 
  2.     echo 'JavaScript局部刷新'
  3.     echo 'JavaScript局部刷新'
  4.     echo 'JavaScript局部刷新'

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章