2015-05-27
好吧先上图,看看,这是 ajax的留言板噢.有瀑布流效果,哈
1.今天学习到了%20jquery的ajax,直接上代码
[php] %20/%20JavaScript%20Document
$(document).ready(function(e)%20{
%20loadHiglight();//载入高亮特效
%20$("#submit").click(function()%20{%20//“留言”按钮单击事件
%20//获取用户名称
%20var%20strUsetName%20=%20$("#userName").val();
%20//获取输入留言内容
%20var%20strContent%20=%20$("#content").val();
%20//获取输入邮箱
%20var%20strEmail%20=%20$("#email").val();
%20//开始发送数据
%20$.ajax({
%20url:%20'index.php?m=index&a=add',
%20type:%20'POST',
%20dataType:%20'json',
%20data:%20{
%20userName:%20strUsetName,
%20content:%20strContent,
%20email:%20strEmail
%20},
%20success:%20function(json,%20textStatus,%20xhr)%20{
%20if%20(json.state=='ok')%20{
%20alert('操作提示,留言成功!');
%20//alert();
%20var%20data=json.data[0];
%20var%20strTag=createTag(data.userName,data.content,data.time);
%20$(strTag).prependTo('div%20#liuyan');
%20//$("<b>Hello%20World!</b>").prependTo("p");
%20}else{
%20alert('操作提示,留言失败!/n错误信息:'+json.error);
%20}
%20}
%20})
%20});
%20//动态载入留言
%20loadMore();
});
//监视滚动条滚动
$(window).scroll(function()%20{
%20//%20当滚动到最底部以上100像素时,%20加载新内容
%20if%20($(document).height()%20-%20$(this).scrollTop()%20-%20$(this).height()%20<%205)%20{
%20loadMore();
%20};
});
fy%20=%200;
function%20loadMore()%20{
%20fy++;
%20//alert(fy);
%20$.getJSON("index.php?m=index&a=data&page="%20+%20fy%20+%20"&rand="%20+%20Math.random(),%20function(json)%20{
%20for%20(var%20i%20=%200;%20i%20<=%20json.length%20-%201;%20i++)%20{
%20//alert(json[i]['userName']);
%20//content%20=%20'<div%20class="content"><div%20class="user"><span%20class="userName">'%20+%20json[i]['userName']%20+%20'</span></div><div%20class="text">'%20+%20json[i]['content']%20+%20'</div><div%20class="time">'%20+%20getLocalTime(json[i]['time'])%20+%20'</div></div>';
%20//content='<div%20class="content">'
%20//alert(content);
%20$("div%20#liuyan").append(createTag(json[i]['userName'],json[i]['content'],json[i]['time']));
%20loadHiglight();
%20};
%20});
}
function%20loadHiglight()%20{//为了ajax后重载特效
%20$user%20=%20$("div%20.user");
%20$text%20=%20$("div%20.text");
%20$("div%20.content").each(function(index)%20{
%20$(this).mousemove(function()%20{
%20$user.eq(index).css("color",%20"#0A8CD2");
%20// %20$user.eq(index).css("background","#FFE6AD");
%20// %20$text.eq(index).css("background","#FFFDF6");
%20}).mouseout(function()%20{
%20$user.eq(index).css("color",%20"#000");
%20// %20$user.eq(index).css("background","#E6F0F9");
%20// %20$text.eq(index).css("background","#F8FBFD");
%20});
%20});
}
function%20createTag(usetName,%20content,%20time)%20{
%20var%20strTag%20=%20'<div%20class="content"><div%20class="user"><span%20class="userName">'%20+%20usetName%20+%20'</span></div><div%20class="text">'%20+%20content%20+%20'</div><div%20class="time">'%20+%20getLocalTime(time)%20+%20'</div></div>';
%20return%20strTag;
}
function%20getLocalTime(nS)%20{
%20return%20new%20Date(parseInt(nS)%20*%201000).toLocaleString().replace(/:/d{1,2}$/,%20'%20');
}
//%20JavaScript%20Document
$(document).ready(function(e)%20{
loadHiglight();//载入高亮特效
$("#submit").click(function()%20{%20//“留言”按钮单击事件
//获取用户名称
var%20strUsetName%20=%20$("#userName").val();
//获取输入留言内容
var%20strContent%20=%20$("#content").val();
//获取输入邮箱
var%20strEmail%20=%20$("#email").val();
//开始发送数据
$.ajax({
url:%20'index.php?m=index&a=add',
type:%20'POST',
dataType:%20'json',
data:%20{
userName:%20strUsetName,
content:%20strContent,
email:%20strEmail
},
success:%20function(json,%20textStatus,%20xhr)%20{
if%20(json.state=='ok')%20{
alert('操作提示,留言成功!');
//alert();
var%20data=json.data[0];
var%20strTag=createTag(data.userName,data.content,data.time);
$(strTag).prependTo('div%20#liuyan');
%20//$("<b>Hello%20World!</b>").prependTo("p");
}else{
alert('操作提示,留言失败!/n错误信息:'+json.error);
}
}
})
});
//动态载入留言
loadMore();
});
//监视滚动条滚动
$(window).scroll(function()%20{
//%20当滚动到最底部以上100像素时,%20加载新内容
if%20($(document).height()%20-%20$(this).scrollTop()%20-%20$(this).height()%20<%205)%20{
loadMore();
};
});
fy%20=%200;
function%20loadMore()%20{
fy++;
//alert(fy);
$.getJSON("index.php?m=index&a=data&page="%20+%20fy%20+%20"&rand="%20+%20Math.random(),%20function(json)%20{
for%20(var%20i%20=%200;%20i%20<=%20json.length%20-%201;%20i++)%20{
//alert(json[i]['userName']);
//content%20=%20'<div%20class="content"><div%20class="user"><span%20class="userName">'%20+%20json[i]['userName']%20+%20'</span></div><div%20class="text">'%20+%20json[i]['content']%20+%20'</div><div%20class="time">'%20+%20getLocalTime(json[i]['time'])%20+%20'</div></div>';
//content='<div%20class="content">'
//alert(content);
$("div%20#liuyan").append(createTag(json[i]['userName'],json[i]['content'],json[i]['time']));
loadHiglight();
};
});
}
function%20loadHiglight()%20{//为了ajax后重载特效
$user%20=%20$("div%20.user");
$text%20=%20$("div%20.text");
$("div%20.content").each(function(index)%20{
$(this).mousemove(function()%20{
$user.eq(index).css("color",%20"#0A8CD2");
// $user.eq(index).css("background","#FFE6AD");
// $text.eq(index).css("background","#FFFDF6");
}).mouseout(function()%20{
$user.eq(index).css("color",%20"#000");
// $user.eq(index).css("background","#E6F0F9");
// $text.eq(index).css("background","#F8FBFD");
});
});
}
function%20createTag(usetName,%20content,%20time)%20{
var%20strTag%20=%20'<div%20class="content"><div%20class="user"><span%20class="userName">'%20+%20usetName%20+%20'</span></div><div%20class="text">'%20+%20content%20+%20'</div><div%20class="time">'%20+%20getLocalTime(time)%20+%20'</div></div>';
return%20strTag;
}
function%20getLocalTime(nS)%20{
return%20new%20Date(parseInt(nS)%20*%201000).toLocaleString().replace(/:/d{1,2}$/,%20'%20');
}请求的方法是
[php] %20function%20data()
{
%20//引入分页类
%20include%20"page.class.php";
%20//得到data表的数据数量
%20$rows%20=%20$this->db->count('select%20*%20from%20data');
%20//创建分页对象
%20$page%20=%20new%20Page($rows,%205,%20"");
%20$list=$this->db
%20->order('id%20DESC')
%20//->table('data')
%20->limit($page->getLimit())
%20->select();
%20/*
%20echo%20"<pre>";
%20var_dump($list);
%20echo%20"</pre>";
%20*/
%20echo%20json_encode($list);
%20exit();
}
function%20data()
{
//引入分页类
include%20"page.class.php";
//得到data表的数据数量
$rows%20=%20$this->db->count('select%20*%20from%20data');
//创建分页对象
$page%20=%20new%20Page($rows,%205,%20"");
$list=$this->db
%20->order('id%20DESC')
%20//->table('data')
%20 %20->limit($page->getLimit())
%20 %20->select();
/*
echo%20"<pre>";
var_dump($list);
echo%20"</pre>";
*/
echo%20json_encode($list);
exit();
}
这样就实现了%20瀑布流效果咯,有点缺点是%20如果加载到%20最后但是却没提示的噢,哈哈哈哈,
[php] %20function%20add(){
%20//添加后返回%20影响条数,如果大于0就说明添加成功
%20$json['state']='no';
%20if%20(empty($_POST['userName']))%20{
%20$json['error']='没有输入用户名';
%20}elseif(empty($_POST['content'])){
%20$json['error']='没有输入留言内容';
%20}elseif(empty($_POST['email'])){
%20$json['error']='没有输入邮箱';
%20}else{
%20isset($_POST['content'])?$_POST['content']=nl2br($_POST['content']):"";
%20$_POST['time']=time();
%20if($this->db->data($_POST)->add()>0){
%20/*
%20echo%20"添加成功";
%20// %20echo%20"<script>location.reload()</script>";//防止刷新后的表单的重复提交
%20Header("HTTP/1.1%20303%20See%20Other");
%20Header("Location:%20"); %20//转向到根目录
%20exit;
%20*/
%20$json['state']='ok';
%20$json['data']=$this
%20->db
%20->table('data')
%20->where('id='.$this->db->last_insert_id())
%20->select();
%20}else%20{
%20$json['error']=$this->db->error();
%20//die($this->db->error());//添加失败输出错误信息
%20}
%20}
%20echo%20json_encode($json);
%20//var_dump($_POST);
%20}
function%20add(){
//添加后返回%20影响条数,如果大于0就说明添加成功
$json['state']='no';
if%20(empty($_POST['userName']))%20{
$json['error']='没有输入用户名';
}elseif(empty($_POST['content'])){
$json['error']='没有输入留言内容';
}elseif(empty($_POST['email'])){
$json['error']='没有输入邮箱';
}else{
isset($_POST['content'])?$_POST['content']=nl2br($_POST['content']):"";
$_POST['time']=time();
if($this->db->data($_POST)->add()>0){
/*
echo%20"添加成功";
// echo%20"<script>location.reload()</script>";//防止刷新后的表单的重复提交
Header("HTTP/1.1%20303%20See%20Other");
Header("Location:%20"); %20//转向到根目录
exit;
*/
$json['state']='ok';
$json['data']=$this
->db
->table('data')
->where('id='.$this->db->last_insert_id())
->select();
}else%20{
$json['error']=$this->db->error();
//die($this->db->error());//添加失败输出错误信息
}
}
echo%20json_encode($json);
//var_dump($_POST);
}这是留言部分的代码,哈哈,这样就可以留言后直接就在页面看到效果,而不用刷新浏览器
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
6
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
7
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
04-12
8
常用dos命令及语法
09-27
PHP中include和require区别之我见
2014-09-05
将视频设置为Android手机开机动画的教程
2014-12-11
php递归返回值的问题
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
java中的info是什么意思
2022-03-24
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
学ug编程如何快速入门?
2022-03-17
PHP数组函数array
2014-09-05
IcePHP框架中的快速后台中的通用CRUD功能框架
2014-09-05
弓箭大师你就是王者游戏下载v2.0.1 安卓最新版
其它手游 164.4M
下载弓箭大师你就是王者官方版下载v2.0.1 安卓版
其它手游 164.4M
下载3d狙击王者手机游戏下载v1.5 安卓版
射击枪战 63.0M
下载足球冲鸭手机版下载v1.0.16.404.401.0116 安卓版
其它手游 81.0M
下载有种来找我手游下载v1.4.8 安卓版
动作闯关 104.7M
下载忍者跳跳跳官方版下载v1.0.2 安卓版
动作闯关 46.8M
下载百战斗斗堂拇指游戏下载v5.8 安卓版
射击枪战 294.3M
下载梦幻斗斗堂手游下载v5.8 安卓版
其它手游 294.3M
下载百战斗斗堂4399版下载v5.8 安卓版
下载
百战斗斗堂九游手游下载v5.8 安卓版
下载
百战斗斗堂手游下载v5.8 安卓正版
下载
弓箭传说bt版下载v2.14.1 安卓版
下载
美味星球永恒手机版下载v1.8.7.0 安卓版
下载
火柴人战争遗产争夺游戏下载v2022.1.9安卓版
下载
史小坑的花前月下游戏下载v1.7.8 安卓版
下载
谁也别想跑小游戏下载v21.01.05 安卓版
下载