学php 有段时间了,今天把自己做的作品拿出来 和大家一起分享下,留言本是用ajax实现, 实现无刷新分页,无刷新留言.
页面演示地址:
http://xlight.freebitty.com/guestbook.php
大家可以参考,把自己的站点做成全无刷新的!
由于是免费空间,有点文字广告, 见凉!
下为代码:
guestbook.php
复制内容到剪贴板
代码:
<?php
header("Content-type: text/html; charset=gbk");
require_once('inc/class_db.php');
require_once('inc/function.php');
$db=new op_db();
?>
<html>
<head><title>留言本</title>
<style type="text/css">
body{text-align:center; margin:0px; background:url(images/bg.gif); color:#000000;}
a:link{text-decoration:none; color:#000000;}
a:visited{text-decoration:none; color:#000000;}
a:hover{text-decoration:none; color:red;}
#head{width:500px; border:0px; text-align:center; margin:0px;}
#head h3{clear:both;}
#head span{float:right;}
#body{width:500px; border:0px dotted #000000;}
#main{width:500px; border:1px dotted #000000;}
#name{width:100%; clear:both; margin:1px 0px;}
#username{width:20%; float:left; text-align:left;}
#username_on{width:80%; float:right; text-align:left;}
#qq{width:100%; border:0px; clear:both; margin:1px 0px;}
#userqq{width:20%; float:left; text-align:left;}
#userqq_on{width:80%; float:right; text-align:left;}
#email{width:100%; border:0px; clear:both; margin:1px 0px;}
#useremail{width:20%; float:left; text-align:left;}
#useremail_on{width:80%; float:right; text-align:left;}
#content{width:100%; float:left; text-align:left; margin:1px 0px;}
#content_on{width:100%; clear:both; background-color:#A6DA9E; text-align:left; margin:1px 0px;}
#fenye{width:500px; text-align:center; clear:both; border:1px dotted #000000;}
.table{border:0px solid #000000; width:500px;}
</style>
<script language="javascript" src="inc/ajax.js"></script>
</head>
<body>
<?php
//处理留言
if(isset($_GET['action'])){
if($_GET['action']=='yes'){
$name=iconv("utf-8","gbk",js_unescape(trim($_GET['name'])));
$qq=intval($_GET['qq']);
$email=trim($_GET['email']);
$content=iconv("utf-8","gbk",js_unescape(trim($_GET['content'])));
$arr=array("admin","xlight");
$errmsg="";
if($name=="" || in_array($name,$arr) || strlen($name)>15){
$errmsg.="姓名长度有误,或含有非法字符!<br>";
}
if($email){
if(ereg("^[a-zA-Z0-9-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]{3,5}$",$email)){
$errmsg.="email的格式不对!<br>";
}
}
else{
$email="";
}
if($content=="" || strlen($content)>200){
$errmsg.="留言内容填写错误!";
}
$time=date("y-m-d h:i:s");
$ip=getenv("REMOTE_ADDR");
if($errmsg==''){
$db->db_query("insert into guest(name,qq,email,ip,time_at,content) values('$name','$qq','$email','$ip','$time','$content')") or die("sorry:" . mysql_error());
}
else{
echo "<font color=red>".$errmsg."</font>";
}
}
}
//解码函数
function js_unescape($str) {
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i++)
{
if ($str[$i] == '%' && $str[$i+1] == 'u')
{
$val = hexdec(substr($str, $i+2, 4));
if ($val < 0x7f) $ret .= chr($val);
else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f));
$i += 5;
}
else if ($str[$i] == '%')
{
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
}
else $ret .= $str[$i];
}
return $ret;
}
//结束
?>
<div id="result"><!--reuslt start-->
<div id="head"><!-- head start-->
<h3>我的留言本</h3>
<span><a href="#liuyan">填写留言</a></span>
</div><!--head end-->
<?php
$page=isset($_GET['page'])?intval($_GET['page']):1;
if($page<1)$page=1;
$pagesize=6; //每页显示6条留言
$rs=$db->db_query("select * from guest");
$num=$db->db_num($rs);//总留言数
$pagenum=ceil($num/$pagesize); //总页数
if($page>$pagenum)$page=$pagenum;
$start=($page-1)*$pagesize;
$result=$db->db_query("select * from guest order by id desc limit $start,$pagesize");
?>
<div id="body"><!--body start-->
<?php
if($num){
while($row=$db->db_fetch($result)){
?><div id="main">
<div id="name">
<div id="username">用户名:</div><div id="username_on"><?php echo $row['name'];?></div>
</div>
<div id="qq">
<div id="userqq">QQ:</div><div id="userqq_on"><?php echo $row['qq'];?></div>
</div>
<div id="email">
<div id="useremail">Email:</div><div id="useremail_on"><?php echo $row['email'];?></div>
</div>
<div id="content">留言内容:</div>
<div id="content_on"><font color="#D58462"><?php echo ubb($row['content']);?></font></div>
</div><p>
<?php
}
}
else{
echo "无留言";
}
?>
</div>
<div id="fenye">
<?php
if($page==1){
echo "<font color=gray>首页|上页|</font>";
}
else{
echo "<a href=\"javascript:dopage('result','guestbook.php?page=1')\">首页</a>|<a href=\"javascript:dopage('result','guestbook.php?page=".($page-1)."')\">上页</a>|";
}
if($page==$pagenum){
echo "<font color=gray>下页|尾页</font>";
}
else{
echo "<a href=\"javascript:dopage('result','guestbook.php?page=".($page+1)."')\">下页|<a href=\"javascript:dopage('result','guestbook.php?page=".$pagenum."')\">尾页</a>";
}
?>
</div>
<a name="liuyan"></a>
<div id="liuyan">
<table cellpadding=0 cellspacing=5px class="table">
<form name="form">
<tr><td colspan=2 align="center" id="chkform">带<font color=red>*</font>的必填</td></tr>
<tr><td width="100" align="left">用户名:</td><td align="left"><input type="text" size=20 id="postname" maxlength=15><font color=red>*</font></td></tr>
<tr><td align="left">email:</td><td><input type="text" size="20" id="postemail"></td></tr>
<tr><td align="left">qq:</td><td><input type="text" size=20 maxlength=9 id="postqq"></td></tr>
<tr><td align="left" colspan=2>留言:<font color=red>*</font></td></tr>
<tr><td align="left" colspan=2>
<textarea rows=8 cols=60 name="postcontent" id="postcontent" onkeyup="document.getElementById('count').innerHTML=this.value.length;" maxlength="200"></textarea>
</td></tr>
<tr><td align="center" colspan=2><input type="button" value="留言" name="submit" onclick="chkpost();"> 最大字数:200 当前字数:<span id="count">0</span></td></tr>
</form>
</table>
</div>
</div>
<body>
</html>ajax.js 代码;
复制内容到剪贴板
代码:
function xml(){
var xml='';
if(window.XMLHttpRequest){
xml=new XMLHttpRequest();
}
else if(window.ActiveXObject){
xml=new ActiveXObject("Microsoft.XMLHttp");
}
if(!xml){
alert("创建xmlhttp失败!");
return false;
}
return xml;
}
//无刷新留言本函数开始
function send_request(url){
/*http_request="";
if(window.XMLHttpRequest){
http_request= new XMLHttpRequest();
}
else if(window.ActiveXObject){
http_request= new ActiveXObject("microsoft.XMLHttp");
}
if(!http_request){
alert("创建失败");
return false;
}*/
http_request=xml();
http_request.onreadystatechange=process;
http_request.open("get",url);
http_request.send(null);
}
//处理返回的信息
function process(){
if(http_request.readyState==4){
if(http_request.status==200){
document.getElementById('result').innerHTML=http_request.responseText ;
}
else{
alert("你所请求的页面不正常");
}
}
}
function dopage(obj,url){
document.getElementById(obj).innerHTML="正在读取数据......";
send_request(url);
//reobj=obj;
}
//over
//检查留言表单
function chkpost(){
var postname=document.getElementById('postname').value
var postemail=document.getElementById('postemail').value;
var postqq=document.getElementById('postqq').value;
var postcontent=document.getElementById('postcontent').value;
if(postname==""){
alert("姓名不能为空!");
document.getElementById('postname').focus();
return false;
}
else if(!/^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]{2,5}$/.test(postemail) && postemail!=''){
alert("email填写有误!\n若想保密,请不填!");
document.getElementById('postemail').focus();
return false;
}
if(postqq!='' && !/^[0-9]{5,9}$/.test(postqq)){
alert("qq填写有误!\n若想保密,请不填!");
document.getElementById('postqq').focus();
return false;
}
else if(postcontent==""){
alert('请填写留言!');
document.getElementById('postcontent').focus();
return false;
}
/*
var postname=document.getElementById('postname').value;
var postqq=document.getElementById('postqq').value;
var postemail=document.getElementById('postemail').value;
var postcontent=document.getElementById('postcontent').value;
*/
else{
document.getElementById('result').innerHTML="正在更新数据......";
send_post('guestbook.php?action=yes&name='+escape(postname)+'&qq='+postqq+'&email='+postemail+'&content='+escape(postcontent));
//location='guestbook.php';
}
}
function send_post(URL){
xml_http=xml();
xml_http.onreadystatechange=processrequest;
xml_http.open("get",URL);
xml_http.send(null);
}
function processrequest(){
if(xml_http.readyState==4){
if(xml_http.status==200){
document.getElementById('result').innerHTML=xml_http.responseText;
}
else{
alert("你所请求的页面不正常!");
}
}
}class_db.php 代码 //连接数据库, 查询表
复制内容到剪贴板
代码:
<?php
require_once('goal.php');
//数据库操作
class op_db{
//构造函数,调用对象就运行
public function __construct(){
$this->link=mysql_connect(db_host,db_user,db_pwd);
mysql_select_db(db);
}
//查询sql
public function db_query($sql){
return mysql_query($sql);
}
//得到记录数
public function db_num($result){
return mysql_num_rows($result);
}
//返回数据库内容成数组
public function db_fetch($result){
return mysql_fetch_array($result);
}
//从数据库中得到记录集数量
public function db_count($string){
$this->result=$this->db_query($string);
$this->row=$this->db_fetch($this->result);
return $this->row[0];
}
//关闭数据库链接
public function __destruct(){
mysql_close($this->link);
}
}
//创建主要内容
class main{
public function main($pagesize,$page,$sql){
$this->pagesize=$pagesize;
$this->page=isset($page)?$page:1;
$this->sql=$sql;
}
}
?>初次做ajax 难免有不当之处.