|
那么首先,我們需要一個基本的ajax開發(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)建對象實例失敗
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;
}
}
有一點ajax基礎(chǔ)的通過注釋,應(yī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ù)庫操作類生成實例
$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//調(diào)用連接參數(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)
<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)頁的一部分,也就是實現(xiàn)這一功能的框架
1.點擊“提交”,開始發(fā)送數(shù)據(jù)
2. 數(shù)據(jù)發(fā)送成功
3. 刷新評論列表
php技術(shù):PHP Ajax實現(xiàn)頁面無刷新發(fā)表評論,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。