简单实用方便的ajax评论完整代码
来源:互联网 发布:淘宝网站外推广 编辑:程序博客网 时间:2024/05/23 12:52
简单的ajax评论完整代码
数据库结构CREATE TABLE `comments` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(128) collate utf8_unicode_ci NOT NULL default '',
`url` varchar(255) collate utf8_unicode_ci NOT NULL default '',
`email` varchar(255) collate utf8_unicode_ci NOT NULL default '',
`body` text collate utf8_unicode_ci NOT NULL,
`dt` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;
演示
PHP
<?php// Error reporting:error_reporting(E_ALL^E_NOTICE);include "conn.php";include "comment.class.php";/* (PS:^_^不错的php学习交流群:276167802,验证:csl,有兴趣的话可以加入进来一起讨论)/ Select all the comments and populate the $comments array with objects*/$comments = array();$result = mysql_query("SELECT * FROM comments ORDER BY id ASC");while($row = mysql_fetch_assoc($result)){$comments[] = new Comment($row);}?>
PHP
<div id="main"><?php/*/ Output the comments one by one:*/foreach($comments as $c){echo $c->markup();}?><div id="addCommentContainer"><p>Add a Comment</p><form id="addCommentForm" method="post" action=""><div><label for="name">Your Name</label><input type="text" name="name" id="name" /><label for="email">Your Email</label><input type="text" name="email" id="email" /><label for="url">Website (not required)</label><input type="text" name="url" id="url" /><label for="body">Comment Body</label><textarea name="body" id="body" cols="20" rows="5"></textarea><input type="submit" id="submit" value="Submit" /></div></form></div></div>submit.php
PHP
<?php// Error reporting:error_reporting(E_ALL^E_NOTICE);include "conn.php";include "comment.class.php";/*/ This array is going to be populated with either/ the data that was sent to the script, or the/ error messages./*/$arr = array();$validates = Comment::validate($arr);if($validates){/* Everything is OK, insert to database: */mysql_query(" INSERT INTO comments(name,url,email,body)VALUES ('".$arr['name']."','".$arr['url']."','".$arr['email']."','".$arr['body']."')");$arr['dt'] = date('r',time());$arr['id'] = mysql_insert_id();/*/ The data in $arr is escaped for the mysql query,/ but we need the unescaped variables, so we apply,/ stripslashes to all the elements in the array:/*/$arr = array_map('stripslashes',$arr);$insertedComment = new Comment($arr);/* Outputting the markup of the just-inserted comment: */echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));}else{/* Outputtng the error messages */echo '{"status":0,"errors":'.json_encode($arr).'}';}?>comment.class.php
PHP
<?phpclass Comment{private $data = array();public function __construct($row){/*/ The constructor*/$this->data = $row;}public function markup(){/*/ This method outputs the XHTML markup of the comment*/// Setting up an alias, so we don't have to write $this->data every time:$d = &$this->data;$link_open = '';$link_close = '';if($d['url']){// If the person has entered a URL when adding a comment,// define opening and closing hyperlink tags$link_open = '<a href="'.$d['url'].'">';$link_close = '</a>';}// Converting the time to a UNIX timestamp:$d['dt'] = strtotime($d['dt']);// Needed for the default gravatar image:$url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';return '<div class="comment"><div class="avatar">'.$link_open.'<img src="" />'.$link_close.'</div><div class="name">'.$link_open.$d['name'].$link_close.'</div><div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div><p>'.$d['body'].'</p></div>';}public static function validate(&$arr){/*/ This method is used to validate the data sent via AJAX.// It return true/false depending on whether the data is valid, and populates/ the $arr array passed as a paremter (notice the ampersand above) with/ either the valid input data, or the error messages.*/$errors = array();$data = array();// Using the filter_input function introduced in PHP 5.2.0if(!($data['email'] = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL))){$errors['email'] = 'Please enter a valid Email.';}if(!($data['url'] = filter_input(INPUT_POST,'url',FILTER_VALIDATE_URL))){// If the URL field was not populated with a valid URL,// act as if no URL was entered at all:$url = '';}// Using the filter with a custom callback function:if(!($data['body'] = filter_input(INPUT_POST,'body',FILTER_CALLBACK,array('options'=>'Comment::validate_text')))){$errors['body'] = 'Please enter a comment body.';}if(!($data['name'] = filter_input(INPUT_POST,'name',FILTER_CALLBACK,array('options'=>'Comment::validate_text')))){$errors['name'] = 'Please enter a name.';}if(!empty($errors)){// If there are errors, copy the $errors array to $arr:$arr = $errors;return false;}// If the data is valid, sanitize all the data and copy it to $arr:foreach($data as $k=>$v){$arr[$k] = mysql_real_escape_string($v);}// Ensure that the email is lower case:$arr['email'] = strtolower(trim($arr['email']));return true;}private static function validate_text($str){/*/ This method is used internally as a FILTER_CALLBACK*/if(mb_strlen($str,'utf8')<1)return false;// Encode all html special characters (<, >, ", & .. etc) and convert// the new line characters to <br> tags:$str = nl2br(htmlspecialchars($str));// Remove the new line characters that are left$str = str_replace(array(chr(10),chr(13)),'',$str);return $str;}}?>
- 简单实用方便的ajax评论完整代码
- 简单的ajax评论完整代码
- 简单的ajax评论
- 十几个方便实用的jquery代码
- 简单实用的ajax脚本
- 简单方便又实用的在线作图工具:ProcessOn
- 简单方便的代码混淆(Android)
- 简单方便设置滚动条的代码
- Python机器学习中的实用小操作(一):数据清理篇 阿萨姆 2 个月前 0. 背景 很多朋友评论希望我共享答案和文章中提到的操作代码,比如生成相关性图谱等。为了方便大家,开始介绍一些虽然简单但很有用
- 一个完整的ajax简单案例
- 一个简单实用的AJAX例子
- 一个简单实用的AJAX例子
- MVC4 AJAX的简单实用方法
- 简单实用的ajax缓存类
- ajax实现局部无刷新的评论代码
- Ajax的优点评论
- [AJAX]Ajax的优点评论
- 如何简单实用ajax
- 【discuzx2】button标签是双闭合标签
- LeetCode题解:Binary Tree Level Order Traversal I and II
- delphi 中关闭进程
- 如何高效利用GitHub
- ubuntu12.04LTS 用到了的命令【持续更新【20131104】
- 简单实用方便的ajax评论完整代码
- I2C设备驱动编写
- UI的旋转
- discuz子导航下面的版块只有图标没有标题,什么原因?
- 索引
- 上下滑屏调节声音、亮度
- u盘装linux结果启动分区在U盘 每次启动必须插上U盘才能进系统 怎么改到硬盘上,硬盘上也有/boot分区
- maven build 失败信息(No goals have been specified for this build)
- 虚拟机中ubuntu 12.10与windows利用samba互相共享文件