PHP Ajax實(shí)現(xiàn)頁面無刷新發(fā)表評論
更新時間:2006年12月02日 00:00:00 作者:
大家都有在網(wǎng)站發(fā)表評論的經(jīng)歷,傳統(tǒng)的發(fā)表過程無非是:發(fā)表->提交頁面表單->等待刷新頁面,這樣在網(wǎng)絡(luò)比較擁擠的時候,往往需要漫長的等待,今天介紹用PHP+Ajax實(shí)現(xiàn)頁面無刷新發(fā)表評論,希望對初學(xué)ajax的PHPer有所幫助。 那么首先,我們需要一個基本的ajax開發(fā)框架,文件ajax.js就包含了這個框架,代碼如下:
var http_request=false;
function send_request(url){//初始化,指定處理函數(shù),發(fā)送請求的函數(shù)
http_request=false;
//開始初始化XMLHttpRequest對象
if(window.XMLHttpRequest){//Mozilla瀏覽器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){//設(shè)置MIME類別
http_request.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject){//IE瀏覽器
try{
http_request=new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
http_request=new ActiveXobject("Microsoft.XMLHttp");
}catch(e){}
}
}
if(!http_request){//異常,創(chuàng)建對象實(shí)例失敗
window.alert("創(chuàng)建XMLHttp對象失??!");
return false;
}
http_request.onreadystatechange=processrequest;
//確定發(fā)送請求方式,URL,及是否同步執(zhí)行下段代碼
http_request.open("GET",url,true);
http_request.send(null);
}
//處理返回信息的函數(shù)
function processrequest(){
if(http_request.readyState==4){//判斷對象狀態(tài)
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>";
return false;
}
else if(newfourm==""){
document.getElementById(obj).innerHTML="<img src=images/false.gif> <font color=red>您還沒填寫評論內(nèi)容!</font>";
return false;
}
else{
document.getElementById(obj).innerHTML="正在發(fā)送數(shù)據(jù)...";
send_request('sendnewfourm.php?username='+username+'&newfourm='+newfourm+'&id='+id);
reobj=obj;
}
}
有一點(diǎn)ajax基礎(chǔ)的通過注釋,應(yīng)該都可以看懂這段代碼,我們可以看出,當(dāng)我們開始發(fā)表評論的時候,在一個特定位置先顯示:正在發(fā)送數(shù)據(jù)...。接著調(diào)用回調(diào)函數(shù)處理數(shù)據(jù)。那么請看服務(wù)器端的代碼:
<?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;//從數(shù)據(jù)庫操作類生成實(shí)例
$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//調(diào)用連接參數(shù)函數(shù)
$db->createcon();//調(diào)用創(chuàng)建連接函數(shù)
$addsql="insert into cr_fourm values(0,'$newfourm','$username','$time',$id)";
$db->query($addsql);
echo"<img src=images/pass.gif> <font color=red>評論已成功發(fā)表!</font>";
//echo $addsql;
$db->close();//關(guān)閉數(shù)據(jù)庫連接
?>
由于jsvascript采用UTF8編碼,在windows下采用ajax回送服務(wù)器的返回信息就會出現(xiàn)亂碼,因此在win下應(yīng)用開頭第一句是非常必要的。中間那段兩個包含文件是數(shù)據(jù)庫操作類和數(shù)據(jù)庫配置信息,我個人習(xí)慣將基本的數(shù)據(jù)庫操作寫成一個類,方便調(diào)用。到這里相信大家已經(jīng)基本明白這個程序的工作原理了,在給出頁面的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"> 快速發(fā)表評論<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="發(fā)表評論" 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>
這是我網(wǎng)頁的一部分,也就是實(shí)現(xiàn)這一功能的框架代碼,顯示評論的頁面用IFRAME(隱藏幀)調(diào)用,待信息發(fā)送完之后,只刷新IFRAME那一塊就可以看到自己發(fā)的評論,從發(fā)送到查看,整個過程都不需要刷新整個頁面。好了,最后看看效果圖吧!^_^
1.點(diǎn)擊“提交”,開始發(fā)送數(shù)據(jù)

2. 數(shù)據(jù)發(fā)送成功

3. 刷新評論列表
復(fù)制代碼 代碼如下:
var http_request=false;
function send_request(url){//初始化,指定處理函數(shù),發(fā)送請求的函數(shù)
http_request=false;
//開始初始化XMLHttpRequest對象
if(window.XMLHttpRequest){//Mozilla瀏覽器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){//設(shè)置MIME類別
http_request.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject){//IE瀏覽器
try{
http_request=new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
http_request=new ActiveXobject("Microsoft.XMLHttp");
}catch(e){}
}
}
if(!http_request){//異常,創(chuàng)建對象實(shí)例失敗
window.alert("創(chuàng)建XMLHttp對象失??!");
return false;
}
http_request.onreadystatechange=processrequest;
//確定發(fā)送請求方式,URL,及是否同步執(zhí)行下段代碼
http_request.open("GET",url,true);
http_request.send(null);
}
//處理返回信息的函數(shù)
function processrequest(){
if(http_request.readyState==4){//判斷對象狀態(tài)
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>";
return false;
}
else if(newfourm==""){
document.getElementById(obj).innerHTML="<img src=images/false.gif> <font color=red>您還沒填寫評論內(nèi)容!</font>";
return false;
}
else{
document.getElementById(obj).innerHTML="正在發(fā)送數(shù)據(jù)...";
send_request('sendnewfourm.php?username='+username+'&newfourm='+newfourm+'&id='+id);
reobj=obj;
}
}
有一點(diǎn)ajax基礎(chǔ)的通過注釋,應(yīng)該都可以看懂這段代碼,我們可以看出,當(dāng)我們開始發(fā)表評論的時候,在一個特定位置先顯示:正在發(fā)送數(shù)據(jù)...。接著調(diào)用回調(diào)函數(shù)處理數(shù)據(jù)。那么請看服務(wù)器端的代碼:
復(fù)制代碼 代碼如下:
<?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;//從數(shù)據(jù)庫操作類生成實(shí)例
$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//調(diào)用連接參數(shù)函數(shù)
$db->createcon();//調(diào)用創(chuàng)建連接函數(shù)
$addsql="insert into cr_fourm values(0,'$newfourm','$username','$time',$id)";
$db->query($addsql);
echo"<img src=images/pass.gif> <font color=red>評論已成功發(fā)表!</font>";
//echo $addsql;
$db->close();//關(guān)閉數(shù)據(jù)庫連接
?>
由于jsvascript采用UTF8編碼,在windows下采用ajax回送服務(wù)器的返回信息就會出現(xiàn)亂碼,因此在win下應(yīng)用開頭第一句是非常必要的。中間那段兩個包含文件是數(shù)據(jù)庫操作類和數(shù)據(jù)庫配置信息,我個人習(xí)慣將基本的數(shù)據(jù)庫操作寫成一個類,方便調(diào)用。到這里相信大家已經(jīng)基本明白這個程序的工作原理了,在給出頁面的HTML代碼:
復(fù)制代碼 代碼如下:
<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"> 快速發(fā)表評論<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="發(fā)表評論" 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>
這是我網(wǎng)頁的一部分,也就是實(shí)現(xiàn)這一功能的框架代碼,顯示評論的頁面用IFRAME(隱藏幀)調(diào)用,待信息發(fā)送完之后,只刷新IFRAME那一塊就可以看到自己發(fā)的評論,從發(fā)送到查看,整個過程都不需要刷新整個頁面。好了,最后看看效果圖吧!^_^
1.點(diǎn)擊“提交”,開始發(fā)送數(shù)據(jù)
2. 數(shù)據(jù)發(fā)送成功
3. 刷新評論列表
您可能感興趣的文章:
- php+ajax實(shí)現(xiàn)無刷新動態(tài)加載數(shù)據(jù)技術(shù)
- php+html5實(shí)現(xiàn)無刷新圖片上傳教程
- PHP+ajax 無刷新刪除數(shù)據(jù)
- php+ajax實(shí)現(xiàn)無刷新分頁的方法
- AJAX PHP無刷新form表單提交的簡單實(shí)現(xiàn)(推薦)
- PHP+AJAX實(shí)現(xiàn)無刷新注冊(帶用戶名實(shí)時檢測)
- PHP Ajax實(shí)現(xiàn)頁面無刷新發(fā)表評論
- ajax實(shí)現(xiàn)無刷新分頁(php)
- php ajax無刷新分頁,支持id定位
- PHP+Ajax無刷新帶進(jìn)度條圖片上傳示例
- php的無刷新操作實(shí)現(xiàn)方法分析
相關(guān)文章
Ajax創(chuàng)建XMLHttp對象的完美兼容性代碼
Ajax創(chuàng)建XMLHttp對象的完美兼容性代碼,需要的朋友可以參考下。2011-11-11淺談Ajax相關(guān)及其優(yōu)缺點(diǎn)
最近在學(xué)習(xí)有關(guān) AJAX 的知識,上網(wǎng)查閱了一些有關(guān) AJAX 的資料,主要是有關(guān) AJAX的技術(shù)簡介及其優(yōu)缺點(diǎn)等。在這里,我想對我收集的資料做一些小小的總結(jié),大部分資料都來自網(wǎng)上,本人在此只做分類總結(jié),希望對和我一樣正在學(xué)習(xí) AJAX 的朋友有一定的幫助。2015-06-06ajax內(nèi)部值外部調(diào)用不了的原因及解決方法
下面小編就為大家?guī)硪黄猘jax內(nèi)部值外部調(diào)用不了的原因及解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06簡單實(shí)現(xiàn)ajax三級聯(lián)動效果
這篇文章主要教大家簡單實(shí)現(xiàn)ajax三級聯(lián)動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10