简单的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 Code
- <?php
- // Error reporting:
- error_reporting(E_ALL^E_NOTICE);
- include "conn.php";
- include "comment.class.php";
- /*
- / 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 Code
- <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 Code
- <?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 Code
- <?php
- class 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.0
- if(!($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评论完整代码.docx简单的ajax评论完整代码.docx简单的ajax评论完整代码.docx
AJAX显示文章列表、AJAX评论、AJAX留言、突出热门显示最新文章、可以划分无限个文章种类、可以制作多个友情链接、评论留言AJAX分页显示、提供文章和评论的RSS源、全局过滤器防SQL注入、后台管理等等。本系统界面友好...
代码简单,较为通用,容易整合到其他系统中, 带有评论支持和反对的投票, 可以引用评论,实现盖楼功能, 简单的验证,有验证码防广告功能. 允许程序在 register_globals = off 的环境下工作 安装:将sql.txt导入到...
www.sendawangluo.com页面获取评论时显示loading加载效果jquery真的是一个非常优秀的JS库,简单容易掌握,对于网页中的多级菜单、级联效果、Tab选项卡切换、图片轮转显示,实现起来都非常的简单,往往就是几句代码的...
实现起来都非常的简单,往往就是几句代码的事。 做AJAX应用,jquery提供的$.get()、$.post()函数都可以用于提交数据,但建议使用$.ajax()来提交,那两个函数都不 提供错误返回信息,不利全面掌控。 提交数据是...
2、简单的代码,方便用户修改。 3、采用了DIV+CSS的实现,页面更简洁,显示速度更快。 系统具有以下功能: 1、普通用户留言功能。 2、管理员删除留言功能。 3、管理员评论留言功能。 4、留言本的验证码功能,防止...
IWEDIT1的事件处理程序启动AJAX调用,将在IWEDIT1输入的文字传到后台,后台的TIWURLResponderEvent响应前台传来的数据,处理完毕后返回前台,通过前台在AJAX调用时注册的回调函数将处理结果显示在屏幕上。...
谈到 WordPress Ajax 就不得不谈到评论 Ajax提交,作为一个博客、论坛评论的 Ajax 提交不仅可以改善用户体验,还可以大幅缩减服务器开支,毕竟输出单条评论内容比重新组织输出一个页面要简单的多。 虽说现在访问量...
Asp+Ajax无限级联动下拉框菜单Access版 ASP 树形菜单TreeView 多样式版 Ajax仿iGoogle双击编辑、网页拖动完整实例 ASP+jQuery无刷新读写数据库操作 Ajax提交数据实例_Ajax+ASP简单留言本 Ajax仿Google搜索提示ASP+...
本文实例讲述了jQuery+ajax简单实现文件上传的方法。分享给大家供大家参考,具体如下: 可以通过ajax来提交表单,而不会刷新页面。主要使用的方法是 $(“#formID”).ajaxSubmit()方法。 1、要引入js插件 需要下载的...
代码简单,较为通用,容易整合到其他系统中, 带有评论支持和反对的投票, 可以引用评论,实现盖楼功能, 简单的验证,有验证码防广告功能. 安装:将sql.txt导入到mysql修改MooPHP/MooConfig.php的配置即可
Html页面代码就这样简单就行了: 代码如下: <body>”room”> </table> <div> 用户名:<input id=”Text1″ type=”text” /> 信息:<textarea id=”TextArea1″ cols=”20″ name=”S1″ rows=”...
南充海涛AJAX文章系统,风格美观大方,功能简约而又实用并以AJAX应用,适用于任何文章、新闻、图片站,代码简单,注释清楚,二次开发和置于其它系统非常方便。 文章显示方式以滑动文章内容方式,简单直观富有灵活感...
南充海涛AJAX文章系统,风格美观大方,功能简约而又实用并以AJAX应用,适用于任何文章、新闻、图片站,代码简单,注释清楚,二次开发和置于其它系统非常方便。 文章显示方式以滑动文章内容方式,简单直观富有灵活...
代码简单明了,一看就懂 绝对好用 可将评分结果ajax同步到数据库
代码简单,较为通用,容易整合到其他系统中, 带有评论支持和反对的投票, 可以引用评论,实现盖楼功能, 简单的验证,有验证码防广告功能. 允许程序在 register_globals = off 的环境下工作 安装:将sql.txt导入到...
Ajax领域的新框架和组件库层出不穷,一些功能非常简单,一些则是过度的设计或者存在着严重的设计问题。Ajax开发者对于应该选择什么样的框架感到茫然无助,毫无疑问,Ajax in Action可以帮助你。本书是目前已经出版的...
8、代码中心,分为了分享代码、下载代码、评论代码、收藏代码。 9、设置功能,支持修改昵称、城市、性别、座右铭、密码、头像。 10、赞助兑换功能,支持1个赞助兑换10个积分,也支持用赞助升级称号。 11、其他功能...
很简单的一个文章管理示例代码,在这个项目中使用了一些asp.net的新特性,例如数据验证功能以及强类型的HtmlHelper等,功能方面只实现了文章列表和添加文章的功能,至于编辑和删除功能实在太简单了,就没有实现,...
都是一些基础的技术,像登录,注册验证用到了ajax和jquery结合,代码更加简单易懂。 后台用了hibernate技术,数据库用了mysql。 页面主要有登录,注册,显示联系人列表的主页面,修改,增加,删除,查找联系人等功能...