php实战第四天_PHP教程
好吧先上图,看看,这是 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()
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
//alert(json[i]['userName']);
//content = '
//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 = '
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()
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
//alert(json[i]['userName']);
//content = '
//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 = '
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 "
";<br> var_dump($list);<br> 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 "
";<br> var_dump($list);<br> 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 "<script>location.reload()</script>";//防止刷新后的表单的重复提交
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 "<script>location.reload()</script>";//防止刷新后的表单的重复提交
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();
说明
返回 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]
<PRE class=javascript name="code">

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen



PHP 8.4 bringt mehrere neue Funktionen, Sicherheitsverbesserungen und Leistungsverbesserungen mit einer beträchtlichen Menge an veralteten und entfernten Funktionen. In dieser Anleitung wird erklärt, wie Sie PHP 8.4 installieren oder auf PHP 8.4 auf Ubuntu, Debian oder deren Derivaten aktualisieren. Obwohl es möglich ist, PHP aus dem Quellcode zu kompilieren, ist die Installation aus einem APT-Repository wie unten erläutert oft schneller und sicherer, da diese Repositorys in Zukunft die neuesten Fehlerbehebungen und Sicherheitsupdates bereitstellen.

Um in cakephp4 mit Datum und Uhrzeit zu arbeiten, verwenden wir die verfügbare FrozenTime-Klasse.

Um am Datei-Upload zu arbeiten, verwenden wir den Formular-Helfer. Hier ist ein Beispiel für den Datei-Upload.

CakePHP ist ein Open-Source-Framework für PHP. Es soll die Entwicklung, Bereitstellung und Wartung von Anwendungen erheblich vereinfachen. CakePHP basiert auf einer MVC-ähnlichen Architektur, die sowohl leistungsstark als auch leicht zu verstehen ist. Modelle, Ansichten und Controller gu

Der Validator kann durch Hinzufügen der folgenden zwei Zeilen im Controller erstellt werden.

Die Anmeldung bei CakePHP ist eine sehr einfache Aufgabe. Sie müssen nur eine Funktion verwenden. Sie können Fehler, Ausnahmen, Benutzeraktivitäten und von Benutzern durchgeführte Aktionen für jeden Hintergrundprozess wie Cronjob protokollieren. Das Protokollieren von Daten in CakePHP ist einfach. Die Funktion log() wird bereitgestellt

Visual Studio Code, auch bekannt als VS Code, ist ein kostenloser Quellcode-Editor – oder eine integrierte Entwicklungsumgebung (IDE) –, die für alle gängigen Betriebssysteme verfügbar ist. Mit einer großen Sammlung von Erweiterungen für viele Programmiersprachen kann VS Code c

CakePHP ist ein Open-Source-MVC-Framework. Es erleichtert die Entwicklung, Bereitstellung und Wartung von Anwendungen erheblich. CakePHP verfügt über eine Reihe von Bibliotheken, um die Überlastung der häufigsten Aufgaben zu reduzieren.
