php无刷新发表评论实现代码
我们先来看看效果图,这是我网页的一部分,也就是实现这一功能的框架代码,显示评论的页面用IFRAME(隐藏帧)调用,待信息发送完之后,只刷新IFRAME那一块就可以看到自己发的评论,从发送到查看,整个过程都不需要刷新整个页面。
1.点击“提交”,开始发送数据
2. 数据发送成功
3. 刷新评论列表
好了,现在我们开始来做代码。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8156')"><textarea id="copy8156" rows="10" cols="40" style="display: none;"></textarea></td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8156>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");
}
}
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){//异常,创建对象实例失败
window.alert("创建XMLHttp对象失败!");
return false;
}
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>";
return false;
}
else if(newfourm==""){
document.getElementById(obj).innerHTML="<img src=images/false.gif> <font color=red>您还没填写评论内容!</font>";
return false;
}
else{
document.getElementById(obj).innerHTML="正在发送数据...";
send_request('sendnewfourm.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>
PHP Ajax实现页面无刷新发表评论,希望对初学ajax的PHPer有所帮助。 我们需要一个基本的ajax开发框架,文件ajax.js就包含了这个框架,ajax.js文件我们第一步就有讲述了,只要大家按上面的方法一步步来操作就可以实现无刷新发评论了
您可能感兴趣的文章:
jquery局部刷新DIV及动态时钟显示iterator无法输出数据的问题
php无刷新发表评论实现代码
javascript 无刷新评论实例
帝国cms提交评论后如何自动生成内容页
使用PHP简单实现类似“畅言”等评论系统
无刷新动态加载数据与滚动条加载适合评论的jquery代码
帝国cms内容页调用评论如何实现
JQuery与JSon实现的无刷新分页代码
php验证码大全(实例分享)
php防止恶意刷新日期怎么改