那么首先,我们需要一个基本的ajax开发框架,文件ajax.js就包含了这个框架,代码如下:
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.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=new db;//从数据库操作类生成实例
$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//调用连接参数函数
$db->createcon();//调用创建连接函数
$addsql="insert into cr_fourm values(0,'$newfourm','$username','$time',$id)";
$db->query($addsql);
echo"<img src=images/pass.gif> <font color=red>评论已成功发表!</font>";
//echo $addsql;
$db->close();//关闭数据库连接
?>
由于jsvascript采用utf8编码,在windows下采用ajax回送服务器的返回信息就会出现乱码,因此在win下应用开头第一句是非常必要的。中间那段两个包含文件是数据库操作类和数据库配置信息,我个人习惯将基本的数据库操作写成一个类,方便调用。到这里相信大家已经基本明白这个程序的工作原理了,在给出页面的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"> 快速发表评论<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>
这是我网页的一部分,也就是实现这一功能的框架代码,显示评论的页面用iframe(隐藏帧)调用,待信息发送完之后,只刷新iframe那一块就可以看到自己发的评论,从发送到查看,整个过程都不需要刷新整个页面。好了,最后看看效果图吧!^_^
1.点击“提交”,开始发送数据
2. 数据发送成功
3. 刷新评论列表
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!