PHP Ajax实现页面无刷新发表评论

雪花徐徐飘下,如芦花,似柳絮,像轻悠悠的鹅毛,无尽无休地飘着,飘着,宛如那美丽的银蝶在院中翩翩起舞,又像一群穿白纱裙的小舞女,伴着天空传来的仙乐,轻轻盈盈地在空中飘舞着,旋转着,跳着动人的舞蹈。那么轻快的身影,那么飘逸的舞姿,每一片晶莹的雪花都像一曲婉转、悠扬、清新的轻音乐,都仿佛是一首轻快、和谐、明丽的小诗。哦,神奇的小雪花……
大家都有在网站发表评论的经历,传统的发表过程无非是:发表->提交页面表单->等待刷新页面,这样在网络比较拥挤的时候,往往需要漫长的等待,今天介绍用PHP+Ajax实现页面无刷新发表评论,希望对初学ajax的PHPer有所帮助。那么首先,我们需要一个基本的ajax开发框架,文件ajax.js就包含了这个框架,代码如下:

varhttp_request=false;
functionsend_request(url){//初始化,指定处理函数,发送请求的函数
http_request=false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=newXMLHttpRequest();
if(http_request.overrideMimeType){//设置MIME类别
http_request.overrideMimeType("text/xml");
}
}
elseif(window.ActiveXObject){//IE浏览器
try{
http_request=newActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
http_request=newActiveXobject("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);
}
//处理返回信息的函数
functionprocessrequest(){
if(http_request.readyState==4){//判断对象状态
if(http_request.status==200){//信息已成功返回,开始处理信息
document.getElementById(reobj).innerHTML=http_request.responseText;
}
else{//页面不正常
alert("您所请求的页面不正常!");
}
}
}
functioncheckfourm(obj){
varf=document.fourm;
varnewfourm=f.newfourm.value;
varusername=f.username.value;
varid=f.id.value;
if(username==""){
document.getElementById(obj).innerHTML="<imgsrc=images/false.gif><fontcolor=red>您必须先登录!</font>";
returnfalse;
}
elseif(newfourm==""){
document.getElementById(obj).innerHTML="<imgsrc=images/false.gif><fontcolor=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=newdb;//从数据库操作类生成实例
$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//调用连接参数函数
$db->createcon();//调用创建连接函数 $addsql="insertintocr_fourmvalues(0,'$newfourm','$username','$time',$id)";
$db->query($addsql);
echo"<imgsrc=images/pass.gif><fontcolor=red>评论已成功发表!</font>";
//echo$addsql;
$db->close();//关闭数据库连接
?>

由于jsvascript采用UTF8编码,在windows下采用ajax回送服务器的返回信息就会出现乱码,因此在win下应用开头第一句是非常必要的。中间那段两个包含文件是数据库操作类和数据库配置信息,我个人习惯将基本的数据库操作写成一个类,方便调用。到这里相信大家已经基本明白这个程序的工作原理了,在给出页面的HTML代码:

<tablewidth="100%"border="0"cellspacing="0"cellpadding="0">
<tr>
<tdalign="center"><?phpecho$rows_p[p_info];?></td>
</tr>
<tr>
<tdalign="center"><br><br><iframeframeborder="0"scrolling="auto"src="showfourm.php?picid=<?=$id;?>"style=HEIGHT:250px;VISIBILITY:inherit;WIDTH:98%;Z-INDEX:2></iframe>
</td>
</tr>
<tr>
<tdalign="center"><br><br>
<divalign="center"id="result"></div>
<formname="fourm">
<tablewidth="100%"border="0"cellspacing="0"cellpadding="0">
<tr>
<tdheight="25">快速发表评论<spanclass="STYLE1">(必须先登陆)用户名:
<inputname="username"type="text"value="<?=$username?>"readonly>
</span></td>
</tr>
<tr>
<tdheight="32"align="center"valign="middle"><textareaname="newfourm"class="f"id="newfourm"></textarea></td>
</tr>
<tr>
<tdheight="32"><inputname="submit"type="button"value="发表评论"onClick="checkfourm('result')">
<inputname="reset"type="reset"id="reset"value="重新填写">
<inputname="id"type="hidden"id="id"value="<?phpecho"$id";?>"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

这是我网页的一部分,也就是实现这一功能的框架代码,显示评论的页面用IFRAME(隐藏帧)调用,待信息发送完之后,只刷新IFRAME那一块就可以看到自己发的评论,从发送到查看,整个过程都不需要刷新整个页面。好了,最后看看效果图吧!^_^
1.点击“提交”,开始发送数据
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt="Click here to open new window\nCTRL+Mouse wheel to zoom in/out";}" onclick="if(!this.resized) {return true;} else {window.open('http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4037814');}" alt="" src="http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4037814" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt="Click here to open new window\nCTRL+Mouse wheel to zoom in/out";}" border=0>
2. 数据发送成功
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt="Click here to open new window\nCTRL+Mouse wheel to zoom in/out";}" onclick="if(!this.resized) {return true;} else {window.open('http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4037844');}" alt="" src="http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4037844" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt="Click here to open new window\nCTRL+Mouse wheel to zoom in/out";}" border=0>
3. 刷新评论列表
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt="Click here to open new window\nCTRL+Mouse wheel to zoom in/out";}" onclick="if(!this.resized) {return true;} else {window.open('http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4037875');}" alt="" src="http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4037875" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt="Click here to open new window\nCTRL+Mouse wheel to zoom in/out";}" border=0>

本文PHP Ajax实现页面无刷新发表评论到此结束。在这个忧伤而明媚的三月,我从我单薄的青春里打马而过,穿过紫堇,穿过木棉,穿过时隐时现的悲喜和无常。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
利用ajax+php实现商品价格计算

Ajax获取php返回json数据动态生成select下拉框的实例

PHP Ajax实现表格实时编辑

Ajax实现phpcms 点赞功能实例代码

Ajax+php数据交互并且局部刷新页面的实现详解