ajax静态页面无刷新添加留言,无刷新分页,翻页综合应用

演示页面: http://www.uptoday.cn/index.htm

纯htm页面,通过ajax实现动态功能。添加留言,翻页,看是否有新留言等,完全不用刷新。

前台代码:

 *****************************************************

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> uptoday.cn </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>
 <SCRIPT LANGUAGE="JavaScript">
 <!--
/*********************
功能: ajax静态页面无刷新留言板
演示:http://www.uptoday.cn/index.htm
作者:乐不思学
主页: http://www.uptoday.cn
Q  Q: 254539697
email:gsx123@gmail.com
有问题请联系我!
************************/

/*改这里*/
var ajax_url="guestbook.php";  //后台处理页面


 var xmlhttp = "";    //将ajax对象实例写在函数外,便于多个ajax函数共用   
           //处理多种浏览器  
 if   (window.XMLHttpRequest)   {   //   Mozilla,   Safari,...  
    xmlhttp =new XMLHttpRequest();              
    }  
 else if(window.ActiveXObject)   {   //   IE   
    xmlhttp =new ActiveXObject("Msxml2.XMLHTTP");   
     } 
function ajax(url,div,func)   //通用ajax函数
  {
   var arg_arr=ajax.arguments;

   argstr=null;
   if(arg_arr.length>2)
   {
      argstr="";
        for(i=3;i<arg_arr.length;i+=2)
           argstr+=arg_arr[i].toString()+"="+(arg_arr[i+1])+"&";
      argstr=argstr.substr(0,argstr.length-1);
      }
    
   xmlhttp.open("POST",url,true);
   xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
       switch(func)
       {
       case "update_reply":
  xmlhttp.onreadystatechange=update_reply;
   break;
       case "reshow":
  xmlhttp.onreadystatechange=reshow;  
   break;
       default :
  xmlhttp.onreadystatechange=ajaxget;    
   break; 
       }
  
   xmlhttp.send(argstr); 

       function ajaxget() //默认状态改变时调用的函数
   {    
     if (xmlhttp.readyState==1)
    {
     document.getElementById(div).innerHTML="刷新中……";
    }
 
          else if(xmlhttp.readyState==4)
    {
      document.getElementById(div).innerHTML=xmlhttp.responseText;
                 
    }
          }
  }

var end_replyshow=false;

 //-->
 </SCRIPT>
 <style>
  body{ background:url(bg.gif);font-size:12px;}
  #right{position:absolute;left:350px;top:15px;width:430px;height:630px;background:#C0DFE0;}

  #myword{width:100%;height:40px;border:4px solid #006666;border-bottom:0px solid #006666;
         padding:5px 3px 3px 16px;
          }
  #word{margin:auto;color:red;font:14px;}
 /*reply*/
 #replyform{border:4px solid #006666;padding:6px 4px 6px 5px;width:320px;z-index:3;}
 #showreply,#addreply{border:4px solid #006666;padding:6px 4px 5px 5px;}
 #showreply{height:560px;border-bottom:0px solid #006666;}

  #tuo{cursor:move;color:blue;}
 .hand,#next,#prev{cursor:hand;}


 .inputpage{width:16px;background:transparent;}
 
  #input{float:left;width:53%;border:0px solid #006666;}
  .text{width:100px;}

  #facearea{float:left;width:47%;border:0px solid #006666;}

  .replylist{list-style-type: none;margin-left:13px;}
  .replylist li{}
  .reply_user{font:bold;color:blue;float:left;}
  .reply_addtime{margin-left:20px;float:left;}
  .reply_qq{margin-left:10px;float:right;margin-right:5px;}
  .reply_email{margin-left:10px;float:right;}
  .reply_ip{margin-right:10px;float:right;}
  .reply_content{clear:both;width:96%;padding:6px 3px 5px 5px;border:1px solid       #006666;height:100px;margin-bottom:12px;}
 </style>


 <BODY>

 
  <div id="right">
    <div id="myword">
      <span id="word">无刷新留言本</span>

 </div>
    <div id="showreply">
      载入中……

 </div>

    <div id="addreply">
     <span id="replycount">共<span id=allnum></span>回复 今日</span>
  <span id="prev" οnclick="showreply('prev');">上一页</span>
    <INPUT TYPE="text" id="page" NAME="page" value="1" class="inputpage">
  <span id="next" οnclick="showreply('next');">下一页</span>
  <span class="hand" οnclick="end_replyshow=false;showreply(1,4);update_count();">刷新</span>
  <span class="hand" οnclick="toreply()" style="color:red;font:bold">我要留言</span>
 </div>

  </div><!-- right --> 
  <div id="replyform" style="display:none;position:absolute;  z-index:auto;left:400px;top:260px;background:white;">
    <span id="tuo">添加留言</span>
    <FORM METHOD=POST ACTION="" name=form1>
    <div id="input" >
      大&nbsp;名:<INPUT TYPE="text" NAME="user" class="text"><BR>
     Q &nbsp;&nbsp;Q:<INPUT TYPE="text" NAME="qq" class="text"><BR>
     Email:<INPUT TYPE="text" NAME="email" class="text"><BR>
    </div>
    <div id="facearea" >
    <INPUT TYPE="radio" NAME="face" id="face"  value="1" checked><IMG SRC="txt/img/face/icon_1.gif" WIDTH="15" HEIGHT="15" BORDER="0" ALT="">
    <INPUT TYPE="radio" NAME="face" id="face" value="2"><IMG SRC="txt/img/face/icon_2.gif" WIDTH="15" HEIGHT="15" BORDER="0" ALT="">
    <INPUT TYPE="radio" NAME="face" id="face" value="3"><IMG SRC="txt/img/face/icon_3.gif" WIDTH="15" HEIGHT="15" BORDER="0" ALT="">
    <INPUT TYPE="radio" NAME="face" id="face" value="4"><IMG SRC="txt/img/face/icon_4.gif" WIDTH="15" HEIGHT="15" BORDER="0" ALT="">
    <INPUT TYPE="radio" NAME="face" id="face" value="5"><IMG SRC="txt/img/face/icon_5.gif" WIDTH="15" HEIGHT="15" BORDER="0" ALT="">
     <INPUT TYPE="radio" NAME="face" id="face" value="6"><IMG SRC="txt/img/face/icon_6.gif" WIDTH="15" HEIGHT="15" BORDER="0" ALT="">
    <INPUT TYPE="radio" NAME="face" id="face" value="7"><IMG SRC="txt/img/face/icon_7.gif" WIDTH="15" HEIGHT="15" BORDER="0" ALT="">
    <INPUT TYPE="radio" NAME="face" id="face" value="8"><IMG SRC="txt/img/face/icon_8.gif" WIDTH="15" HEIGHT="15" BORDER="0" ALT="">
    <INPUT TYPE="radio" NAME="face" id="face" value="9"><IMG SRC="txt/img/face/icon_9.gif" WIDTH="15" HEIGHT="15" BORDER="0" ALT="">
    </div>

    <TEXTAREA NAME="content" ROWS="5" COLS="40"></TEXTAREA><BR>
 
    <INPUT TYPE="button" NAME="" value="保存" οnclick="savereply(user.value,content.value,qq.value,email.value)">
    <INPUT TYPE="button" NAME="" value="关闭" οnclick="document.getElementById('replyform').style.display='none';">
   </FORM>
  </div><!-- replyform -->
<SCRIPT LANGUAGE="JavaScript">
<!--


  setTimeout("showreply()",1000);
  
   function showreply(action,num) //显示留言
   {
    if (num==null){num=4;}
    var page=parseInt(document.getElementById("page").value);
 
     switch(action)
     {
     case "next":
   page=page+1;
         if (page>parseInt(document.getElementById("allnum").innerHTML)/num+1){return;}
  break;
     case "prev":
   page=page-1;
   if (page<=0){return;}
     break;
  default:
         page=1;
  break;
 
     }
     sql=" limit "+(page-1)*num+","+num;
 
     ajax(ajax_url,'','reshow','action','show','sql',sql);
     document.getElementById("page").value=page;
   }
  
 

 function toreply(id,page)
 {
  document.getElementById("replyform").style.display='block';
 }

  function savereply(user,content,qq,email) //ajax,提交留言的表单
   {
  
    for(var i=0;i<document.form1.face.length;i++)
        if(document.form1.face[i].checked)
     face=document.form1.face[i].value;

   
    if (user==""||content=="")
   {
   alert("请填写大名和留言!");
      return;
   }
  
   end_replyshow=false;
   ajax(ajax_url,'','update_reply','action','add','user',user,'content',content,'qq',qq,'email',email,'face',face);
  }
           function update_reply() //ajax返回数据后更新页面
            {
       if (xmlhttp.readyState==1)
    {
     document.getElementById("tuo").innerHTML="添加留言中……";
    }
 
          else if(xmlhttp.readyState==4)
    {
      document.getElementById("showreply").innerHTML=xmlhttp.responseText;
                  showreply(1,4);update_count();
                  document.getElementById("content").innerHTML="";
     document.getElementById("tuo").innerHTML="Success!";
     setTimeout("display()",1500);
               
    }
 
            }

   function display()
   {
    document.getElementById('replyform').style.display='none';
    document.getElementById("tuo").innerHTML="添加留言";
   }

           function reshow() //
           {
       if (xmlhttp.readyState==1)
    {
     document.getElementById("showreply").innerHTML="加载中……";
    }
 
          else if(xmlhttp.readyState==4)
    {
      document.getElementById("showreply").innerHTML=xmlhttp.responseText;
                   end_replyshow=true;
    }
 
            }
  
function update_count(){ //更新留言数
  if (end_replyshow==true)
    getcount();
  else
    setTimeout("update_count()",3000);  
  }
function getcount(){
   ajax(ajax_url,'replycount','dd','action','getcount');
  }

  update_count();


//********************************
function drag(o1,o2){//拖动
 o1.onselectstart=function() {return(false)};
 o1.οnmοusedοwn=function(e){
  e=e||window.event;
  e.cancelBubble = true;
  var x=e.layerX||e.offsetX;
  var y=e.layerY||e.offsetY;
  document.οnmοusemοve=function(e){
   e=e||window.event;
   o2.style.left=(e.clientX-x)+"px";
   o2.style.top=(e.clientY-y)+"px";
  };
  document.οnmοuseup=function(){document.οnmοusemοve=null;};
 };
}
drag(document.getElementById("tuo"),document.getElementById("replyform"));


//-->
</SCRIPT>
 </BODY>
</HTML>

*************************************************************

后台关键代码 guestbook.php

###############################

<?php
header('Content-Type:text/html;charset=GB2312');
require("txt/inc/mysql.inc.php");
mysql_select_db("guestbook",$conn);

$action=$_REQUEST['action'];
If (!empty($_REQUEST['sql']))$sql=$_REQUEST['sql'];
If (!empty($_POST["content"]))
{
$user=clean_str(iconv("UTF-8","gbk",$_POST["user"]));
If (empty($user))$user=get_client_ip();
$content=clean_str(iconv("UTF-8","gbk",$_POST["content"]));
$email=$_POST["email"];
$qq=$_POST["qq"];
$face=$_POST["face"];
//echo $content;


}

 

function isShow($type,$what)
  {
    If (!empty($what))
       switch($type)
       {
       case "qq":
  return "<A HREF=/"/"><IMG SRC=/"txt/img/small/qq.gif/" WIDTH=/"20/" HEIGHT=/"16/" BORDER=/"0/" ALT=/"qq:".$what."/"></A>";
   break;
       case "email":
  return "<A HREF=/"mailto:".$what."/"><IMG SRC=/"txt/img/small/email.gif/" WIDTH=/"20/" HEIGHT=/"16/" BORDER=/"0/" ALT=/"".$what."/"></A>";
   break;
       case "ip":
  return "<A HREF=/"http://www.ip138.com/ips.asp?ip=".$what."/" target=_blank><IMG SRC=/"txt/img/small/ip.gif/" WIDTH=/"14/" HEIGHT=/"14/" BORDER=/"0/" ALT=/"".$what."/"></A>";  
   break; 
       }
    else
     return "";
  }

    switch($action)    //
    {
    case "show":   //显示
   $sql="select * from 留言数据表 order by id desc ".$sql;
   $result=mysql_query($sql) or die("无数据");
   while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
         echo "<ul class=replylist>";
         echo "<li class=reply_user>".$row['id']."楼:<span >".$row['user']."</span></li>";
         echo "<li class=reply_addtime>".$row['addtime']."</li>";
         echo "<li class=reply_ip>".isShow('ip',$row['ip'])."</li>";
         echo "<li class=reply_qq>".isShow('qq',$row['qq'])."</li>";
         echo "<li class=reply_email>".isShow('email',$row['email'])."</li>";

         echo "<li class=reply_content><IMG SRC=/"txt/img/face/icon_".$row['face'].".gif/" WIDTH=/"15/" HEIGHT=/"15/" BORDER=/"0/" ALT=/"/"> ".($row['content'])."</li>";
         echo "</ul>";
       }
   break;

    case "add":   //添加
  $sql="insert into 留言数据表 (user,content,qq,email,ip,addtime,face) values ('".$user."','".$content."','".$qq."','".$email."','".get_client_ip()."','".date("Y-m-d H:i:s",time())."','".$face."') ";
  echo $sql;
     $result=mysql_query($sql)  or die("<FONT  COLOR=red>ERROR: </FONT>".mysql_error());
   break;

    case "getcount":   //统计
   echo "共<span id=allnum>".getnum("guestbook")."</span>回复 今日".getnum("guestbook where addtime>'".date("Y-m-d H:i:s",time()-24*60*60) ."'")."";
   break;

    case "other":
  
   break;   
    }


 function   clean_str($val)   {  
  if   ($val   ==   "")  
          {  
          return   "";  
          }  
  $val   =   trim($val);  
                    $val   =   str_replace("&#032;","   ",$val);  
  $val   =   str_replace("&"  , "&amp;" ,   $val   );  
  $val   =   str_replace("<!--" , "&#60;&#33;--",  $val);  
  $val   =   str_replace("-->"   , "--&#62;" ,  $val);  
  $val   =   preg_replace("/<script/i","&#60;script" , $val);  
  $val   =   str_replace(">"  ,   "&gt;" , $val);  
  $val   =   str_replace("<"  ,   "&lt;",   $val);  
  $val   =   str_replace("/""  ,   "&quot;" , $val);  
  $val   =   preg_replace("//|/",   "&#124;", $val);  
  $val   =   preg_replace("//n/",   "<br>",   $val);   //   Convert   literal   newlines  
  $val   =   preg_replace("$/" ,"&#036;",$val);  
  $val   =   preg_replace("//r/",   ""   ,   $val);   //   Remove   literal   carriage   returns  
  $val   =   str_replace("!","&#33;" , $val);  
  $val   =   str_replace("'", "&#39;" , $val);   //   IMPORTANT:   It   helps   to   increase   sql   query   afety.  
  $val   =   stripslashes($val);                                                                           //   Swop   PHP   added   backslashes  
  $val   =   preg_replace("/","&#092;",$val);   //   Swop   user   inputted   backslashes
  $val   =   str_replace("  "," ",$val);
  return   $val;  
 }

?>

##############################

欢迎测试……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值