ps:php+ajax实现页面无刷新发表评论,希望对初学ajax的phper有所帮助。 我们需要一个基本的ajax开发框架,文件ajax.js就包含了这个框架,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= 您必须先登录!;
return false;
}
else if(newfourm==){
document.getelementbyid(obj).innerhtml= 您还没填写评论内容!;
return false;
}
else{
document.getelementbyid(obj).innerhtml=正在发送数据...;
send_request('sendnewfourm.php?username='+username+'&newfourm='+newfourm+'&id='+id);
reobj=obj;
}
}下面是服务器端的代码mysql($dbhost,$dbuser,$dbpassword,$dbname);//调用连接参数函数
$db->createcon();//调用创建连接函数
$addsql=insert into cr_fourm values(0,'$newfourm','$username','$time',$id);
$db->query($addsql);
echo 评论已成功发表!;
//echo $addsql;
$db->close();//关闭数据库连接
?>在给出页面的html代码
style=height:250px;visibility:inherit;width:98%;z-index:2 >
快速发表评论(必须先登陆)用户名:
readonly>
>
ad:真正免费,域名+虚机+企业邮箱=0元
