1. 程式人生 > >Ajax實現頁面無刷新發表評論 for Php

Ajax實現頁面無刷新發表評論 for Php

大家都有在網站發表評論的經歷,傳統的發表過程無非是:發表->提交頁面表單->等待重新整理頁面,這樣在網路比較擁擠的時候,往往需要漫長的等待,今天介紹用PHP+Ajax實現頁面無刷新發表評論,希望對初學ajax的PHPer有所幫助。    那麼首先,我們需要一個基本的ajax開發框架,檔案ajax.js就包含了這個框架,程式碼如下 var http_request=false;
   
function send_request(url){//初始化,指定處理函式,傳送請求的函式     http_request=false;
//開始初始化XMLHttpRequest物件if(window.XMLHttpRequest){//Mozilla瀏覽器   http_request=new XMLHttpRequest();
   
if(http_request.overrideMimeType){//設定MIME類別     http_request.overrideMimeType("text/xml");
   }
}
elseif(window.ActiveXObject){
//IE瀏覽器try{
    http_request
=new ActiveXObject("Msxml2.XMLHttp");
   }
catch(e){
    
try{
    http_request
=new ActiveXobject("Microsoft.XMLHttp");
    }
catch(e){}
   }
     }
if(!http_request){//異常,建立物件例項失敗   window.alert("建立XMLHttp物件失敗!");
   
returnfalse;
}
http_request
.onreadystatechange=processrequest;
//確定傳送請求方式,URL,及是否同步執行下段程式碼
     http_request.open("GET",url,true);
http_request
.send(null);
   }
   
//處理返回資訊的函式function processrequest(){
    
if(http_request.readyState==4){//判斷物件狀態if(http_request.status==200){//資訊已成功返回,開始處理資訊    document.getElementById(reobj).innerHTML=http_request.responseText;
   }
   
else{//頁面不正常    alert("您所請求的頁面不正常!");
   }
    }
   }
    
function checkfourm(obj){
     
var f=document.fourm;
     
var newfourm=f.newfourm.value;
     
var username=f.username.value;
     
var id=f.id.value;
     
if(username==""){
            document
.getElementById(obj).innerHTML="<img src=images/false.gif> <font color=red>您必須先登入!</font>";
      
returnfalse;
     }
     
elseif(newfourm==""){
      document
.getElementById(obj).innerHTML="<img src=images/false.gif> <font color=red>您還沒填寫評論內容!</font>";
      
returnfalse;
     }
     
else{
      document
.getElementById(obj).innerHTML="正在傳送資料...";
      send_request(
'sendnewfourm.php?username='+username+'&newfourm='+newfourm+'&id='+id);
      reobj
=obj;
     }
    }
 有一點ajax基礎的通過註釋,應該都可以看懂這段程式碼,我們可以看出,當我們開始發表評論的時候,在一個特定位置先顯示:正在傳送資料...。接著呼叫回撥函式處理資料。那麼請看伺服器端的程式碼:
<?php
   
header('Content-Type:text/html;charset=GB2312');//避免輸出中文亂碼,linux下不需要$username=trim($_GET['username']);
   
$newfourm=trim($_GET['newfourm']);
   
$id=$_GET['id'];
   
$time=date("Y-m-d");
   
   
include('inc/config.inc.php');
   
include('inc/dbclass.php');
   
$db=new db;//從資料庫操作類生成例項$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//呼叫連線引數函式$db->createcon();//呼叫建立連線函式 $addsql="insert into cr_fourm values(0,'$newfourm','$username','$time',$id)";
   
$db->query($addsql);
   
echo"<img src=images/pass.gif> <font color=red>評論已成功發表!</font>";
   
//echo $addsql;$db->close();//關閉資料庫連線?>

由於jsvascript採用UTF8編碼,在windows下采用ajax回送伺服器的返回資訊就會出現亂碼,因此在win下應用開頭第一句是非常必要的。中間那段兩個包含檔案是資料庫操作類和資料庫配置資訊,我個人習慣將基本的資料庫操作寫成一個類,方便呼叫。到這裡相信大家已經基本明白這個程式的工作原理了,在給出頁面的HTML程式碼:

<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="center"><?php echo$rows_p[p_info];?></td></tr><tr><td align="center"><br><br><iframe frameborder="0" scrolling="auto" src="showfourm.php?picid=<?=$id;?>" style=HEIGHT:250px;VISIBILITY:inherit;WIDTH:98%;Z-INDEX:2></iframe></td></tr><tr><td align="center"><br><br><div align="center" id="result"></div><form name="fourm"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="25"> 快速發表評論<span class="STYLE1">(必須先登陸)使用者名稱:
                 
<input name="username" type="text" value="<?=$username?>" readonly></span></td></tr><tr><td height="32" align="center" valign="middle"><textarea name="newfourm"class="f" id="newfourm"></textarea></td></tr><tr><td height="32"><input name="submit" type="button" value="發表評論" onClick="checkfourm('result')"><input name="reset" type="reset" id="reset" value="重新填寫"><input name="id" type="hidden" id="id" value="<?php echo"$id";?>"></td></tr></table></form></td></tr></table>

這是我網頁的一部分,也就是實現這一功能的框架程式碼,顯示評論的頁面用IFRAME(隱藏幀)呼叫,待資訊傳送完之後,只重新整理IFRAME那一塊就可以看到自己發的評論,從傳送到檢視,整個過程都不需要重新整理整個頁面。好了,最後看看效果圖吧!^_^
    1.點選“提交”,開始傳送資料

2. 資料傳送成功

3. 重新整理評論列表