好吧先,看看,這是 ajax的留言板噢.有瀑布流效果,哈
1.今天學習到了 jquery的ajax,直接上代碼
[php] / JavaScript Document
$(document).ready(function(e) {
loadHiglight();//載入高亮特效
$("#submit").click(function() { //“留言”按鈕單擊事件
//擷取使用者名稱稱
var strUsetName = $("#userName").val();
//擷取輸入留言內容
var strContent = $("#content").val();
//擷取輸入郵箱
var strEmail = $("#email").val();
//開始發送資料
$.ajax({
url: 'index.php?m=index&a=add',
type: 'POST',
dataType: 'json',
data: {
userName: strUsetName,
content: strContent,
email: strEmail
},
success: function(json, textStatus, xhr) {
if (json.state=='ok') {
alert('操作提示,留言成功!');
//alert();
var data=json.data[0];
var strTag=createTag(data.userName,data.content,data.time);
$(strTag).prependTo('div #liuyan');
//$("Hello World!").prependTo("p");
}else{
alert('操作提示,留言失敗!\n錯誤資訊:'+json.error);
}
}
})
});
//動態載入留言
loadMore();
});
//監視捲軸滾動
$(window).scroll(function() {
// 當滾動到最底部以上100像素時, 載入新內容
if ($(document).height() - $(this).scrollTop() - $(this).height() < 5) {
loadMore();
};
});
fy = 0;
function loadMore() {
fy++;
//alert(fy);
$.getJSON("index.php?m=index&a=data&page=" + fy + "&rand=" + Math.random(), function(json) {
for (var i = 0; i <= json.length - 1; i++) {
//alert(json[i]['userName']);
//content = '' + json[i]['userName'] + '' + json[i]['content'] + '' + getLocalTime(json[i]['time']) + '';
//content=''
//alert(content);
$("div #liuyan").append(createTag(json[i]['userName'],json[i]['content'],json[i]['time']));
loadHiglight();
};
});
}
function loadHiglight() {//為了ajax後重載特效
$user = $("div .user");
$text = $("div .text");
$("div .content").each(function(index) {
$(this).mousemove(function() {
$user.eq(index).css("color", "#0A8CD2");
// $user.eq(index).css("background","#FFE6AD");
// $text.eq(index).css("background","#FFFDF6");
}).mouseout(function() {
$user.eq(index).css("color", "#000");
// $user.eq(index).css("background","#E6F0F9");
// $text.eq(index).css("background","#F8FBFD");
});
});
}
function createTag(usetName, content, time) {
var strTag = '' + usetName + '' + content + '' + getLocalTime(time) + '';
return strTag;
}
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
}
// JavaScript Document
$(document).ready(function(e) {
loadHiglight();//載入高亮特效
$("#submit").click(function() { //“留言”按鈕單擊事件
//擷取使用者名稱稱
var strUsetName = $("#userName").val();
//擷取輸入留言內容
var strContent = $("#content").val();
//擷取輸入郵箱
var strEmail = $("#email").val();
//開始發送資料
$.ajax({
url: 'index.php?m=index&a=add',
type: 'POST',
dataType: 'json',
data: {
userName: strUsetName,
content: strContent,
email: strEmail
},
success: function(json, textStatus, xhr) {
if (json.state=='ok') {
alert('操作提示,留言成功!');
//alert();
var data=json.data[0];
var strTag=createTag(data.userName,data.content,data.time);
$(strTag).prependTo('div #liuyan');
//$("Hello World!").prependTo("p");
}else{
alert('操作提示,留言失敗!\n錯誤資訊:'+json.error);
}
}
})
});
//動態載入留言
loadMore();
});
//監視捲軸滾動
$(window).scroll(function() {
// 當滾動到最底部以上100像素時, 載入新內容
if ($(document).height() - $(this).scrollTop() - $(this).height() < 5) {
loadMore();
};
});
fy = 0;
function loadMore() {
fy++;
//alert(fy);
$.getJSON("index.php?m=index&a=data&page=" + fy + "&rand=" + Math.random(), function(json) {
for (var i = 0; i <= json.length - 1; i++) {
//alert(json[i]['userName']);
//content = '' + json[i]['userName'] + '' + json[i]['content'] + '' + getLocalTime(json[i]['time']) + '';
//content=''
//alert(content);
$("div #liuyan").append(createTag(json[i]['userName'],json[i]['content'],json[i]['time']));
loadHiglight();
};
});
}
function loadHiglight() {//為了ajax後重載特效
$user = $("div .user");
$text = $("div .text");
$("div .content").each(function(index) {
$(this).mousemove(function() {
$user.eq(index).css("color", "#0A8CD2");
// $user.eq(index).css("background","#FFE6AD");
// $text.eq(index).css("background","#FFFDF6");
}).mouseout(function() {
$user.eq(index).css("color", "#000");
// $user.eq(index).css("background","#E6F0F9");
// $text.eq(index).css("background","#F8FBFD");
});
});
}
function createTag(usetName, content, time) {
var strTag = '' + usetName + '' + content + '' + getLocalTime(time) + '';
return strTag;
}
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
}請求的方法是
[php] function data()
{
//引入分頁類
include "page.class.php";
//得到data表的資料數量
$rows = $this->db->count('select * from data');
//建立分頁對象
$page = new Page($rows, 5, "");
$list=$this->db
->order('id DESC')
//->table('data')
->limit($page->getLimit())
->select();
/*
echo "
";
var_dump($list);
echo "
";
*/
echo json_encode($list);
exit();
}
function data()
{
//引入分頁類
include "page.class.php";
//得到data表的資料數量
$rows = $this->db->count('select * from data');
//建立分頁對象
$page = new Page($rows, 5, "");
$list=$this->db
->order('id DESC')
//->table('data')
->limit($page->getLimit())
->select();
/*
echo "
";
var_dump($list);
echo "
";
*/
echo json_encode($list);
exit();
}
這樣就實現了 瀑布流效果咯,有點缺點是 如果載入到 最後但是卻沒提示的噢,哈哈哈哈,
[php] function add(){
//添加後返回 影響條數,如果大於0就說明添加成功
$json['state']='no';
if (empty($_POST['userName'])) {
$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 "添加成功";
// echo "";//防止重新整理後的表單的重複提交
Header("HTTP/1.1 303 See Other");
Header("Location: "); //轉向到根目錄
exit;
*/
$json['state']='ok';
$json['data']=$this
->db
->table('data')
->where('id='.$this->db->last_insert_id())
->select();
}else {
$json['error']=$this->db->error();
//die($this->db->error());//添加失敗輸出錯誤資訊
}
}
echo json_encode($json);
//var_dump($_POST);
}
function add(){
//添加後返回 影響條數,如果大於0就說明添加成功
$json['state']='no';
if (empty($_POST['userName'])) {
$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 "添加成功";
// echo "";//防止重新整理後的表單的重複提交
Header("HTTP/1.1 303 See Other");
Header("Location: "); //轉向到根目錄
exit;
*/
$json['state']='ok';
$json['data']=$this
->db
->table('data')
->where('id='.$this->db->last_insert_id())
->select();
}else {
$json['error']=$this->db->error();
//die($this->db->error());//添加失敗輸出錯誤資訊
}
}
echo json_encode($json);
//var_dump($_POST);
}這是留言部分的代碼,哈哈,這樣就可以留言後直接就在頁面看到效果,而不用重新整理瀏覽器囖,效果漂亮多了 jquery+ajax就是牛X.哈
2.今天學習到的php 函數
[php] json_encode();
說明
string
json_encode ( mixed
$value [, int
$options = 0 ] )
返回 value 值的 JSON 形式
empty() 如果值是空的話,返回真,有值就返回假,
time() 取得時間戳記
mysql_insert_id() 返回上一次操作自增欄位的id
json_encode(); 說明string json_encode ( mixed $value [, int $options = 0 ] )返回 value 值的 JSON 形式empty() 如果值是空的話,返回真,有值就返回假,
time() 取得時間戳記
mysql_insert_id() 返回上一次操作自增欄位的id
3.今天學習到的 javascript函數,這函數用於轉換時間戳記.
[javascript] function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
}
jquery的append()用於載入到標籤內最後面的HTML
prependTo()載入HTML代碼到 標籤的最前面
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
}
jquery的append()用於載入到標籤內最後面的HTML
prependTo()載入HTML代碼到 標籤的最前面
4.今天得到的經驗ajax載入網頁標籤,後jquery特效會消失,所以在ajax載入網頁標籤後,要重新綁定 jquery的事件和函數。不然特效就沒咯。
[javascript]
http://www.bkjia.com/PHPjc/477418.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477418.htmlTechArticle好吧先,看看,這是 ajax的留言板噢.有瀑布流效果,哈 1.今天學習到了 jquery的ajax,直接上代碼 [php] / JavaScript Document $(document).ready(function(e...