站长论坛

标题: PHP Ajax实现页面无刷新发表评论 [打印本页]

作者: tznktg    时间: 2007-9-19 23:15
标题: PHP Ajax实现页面无刷新发表评论
传统的发表过程无非是:发表->提交页面表单->等待刷新页面,这样在网络比较拥挤的时候,往往需要漫长的等待,今天介绍用PHP+Ajax 实现页面无刷新发表评论,希望对初学ajax的PHPer有所帮助。  那么首先,我们需要一个基本的ajax开发框架,文件ajax.js就包含了这个框架,代码如下:
  1. var http_request=false;
  2.   function send_request(url){//初始化,指定处理函数,发送请求的函数
  3.     http_request=false;
  4. //开始初始化XMLHttpRequest对象
  5. if(window.XMLHttpRequest){//Mozilla浏览器
  6.   http_request=new XMLHttpRequest();
  7.   if(http_request.overrideMimeType){//设置MIME类别
  8.     http_request.overrideMimeType("text/xml");
  9.   }
  10. }
  11. else if(window.ActiveXObject){//IE浏览器
  12.   try{
  13.   http_request=new ActiveXObject("Msxml2.XMLHttp");
  14.   }catch(e){
  15.   try{
  16.   http_request=new ActiveXobject("Microsoft.XMLHttp");
  17.   }catch(e){}
  18.   }
  19.     }
  20. if(!http_request){//异常,创建对象实例失败
  21.   window.alert("创建XMLHttp对象失败!");
  22.   return false;
  23. }
  24. http_request.onreadystatechange=processrequest;
  25. //确定发送请求方式,URL,及是否同步执行下段代码
  26.     http_request.open("GET",url,true);
  27. http_request.send(null);
  28.   }
  29.   //处理返回信息的函数
  30.   function processrequest(){
  31.   if(http_request.readyState==4){//判断对象状态
  32.     if(http_request.status==200){//信息已成功返回,开始处理信息
  33.   document.getElementById(reobj).innerHTML=http_request.responseText;
  34.   }
  35.   else{//页面不正常
  36.   alert("您所请求的页面不正常!");
  37.   }
  38.   }
  39.   }
  40.   function checkfourm(obj){
  41.     var f=document.fourm;
  42.     var newfourm=f.newfourm.value;
  43.     var username=f.username.value;
  44.     var id=f.id.value;
  45.     if(username==""){
  46.           document.getElementById(obj).innerHTML=" 您必须先登录!";
  47.     return false;
  48.     }
  49.     else if(newfourm==""){
  50.     document.getElementById(obj).innerHTML=" 您还没填写评论内容!";
  51.     return false;
  52.     }
  53.     else{
  54.     document.getElementById(obj).innerHTML="正在发送数据...";
  55.     send_request('sendnewfourm.php?username='+username+'&newfourm='+newfourm+'&id='+id);
  56.     reobj=obj;
  57.     }
  58.   }
复制代码

作者: tznktg    时间: 2007-9-19 23:16
有一点ajax基础的通过注释,应该都可以看懂这段代码,我们可以看出,当我们开始发表评论的时候,在一个特定位置先显示:正在发送数据...。接着调用回调函数处理数据。那么请看服务器端的代码:
  1. <?php
  2.   header('Content-Type:text/html;charset=GB2312');//避免输出中文乱码,linux下不需要
  3.   $username=trim($_GET['username']);
  4.   $newfourm=trim($_GET['newfourm']);
  5.   $id=$_GET['id'];
  6.   $time=date("Y-m-d");
  7.   
  8.   include('inc/config.inc.php');
  9.   include('inc/dbclass.php');
  10.   $db=new db;//从数据库操作类生成实例
  11.   $db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//调用连接参数函数
  12.   $db->createcon();//调用创建连接函数
  13.   
  14.   $addsql="insert into cr_fourm values(0,'$newfourm','$username','$time',$id)";
  15.   $db->query($addsql);
  16.   echo" 评论已成功发表!";
  17.   //echo $addsql;
  18.   $db->close();//关闭数据库连接
  19. ?>
复制代码
由于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>





欢迎光临 站长论坛 (http://tzlink.com/bbs/) Powered by Discuz! X3.2