PHP Ajax實現(xiàn)頁面無刷新發(fā)表評論
那么首先,我們需要一個基本的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)該都可以看懂這段代碼,我們可以看出,當(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ù)庫操作類生成實例
$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+ajax實現(xiàn)無刷新動態(tài)加載數(shù)據(jù)技術(shù)
- php+html5實現(xiàn)無刷新圖片上傳教程
- PHP+ajax 無刷新刪除數(shù)據(jù)
- php+ajax實現(xiàn)無刷新分頁的方法
- AJAX PHP無刷新form表單提交的簡單實現(xiàn)(推薦)
- PHP+AJAX實現(xiàn)無刷新注冊(帶用戶名實時檢測)
- PHP Ajax實現(xiàn)頁面無刷新發(fā)表評論
- ajax實現(xiàn)無刷新分頁(php)
- php ajax無刷新分頁,支持id定位
- PHP+Ajax無刷新帶進(jìn)度條圖片上傳示例
- php的無刷新操作實現(xiàn)方法分析
相關(guān)文章
Warning: session_destroy() : Trying to destroy uninitialized
在使用session_destroy(),進(jìn)行Session變量的注銷時,出現(xiàn)了Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in的錯誤!2011-06-06php使用ZipArchive提示Fatal error: Class ZipArchive not found in的
這篇文章主要介紹了php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解決方法,是使用ZipArchive時經(jīng)常會遇到的問題,需要的朋友可以參考下2014-11-11