`

简单的ajax评论完整代码

 
阅读更多

简单的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
  1. <?php  
  2.   
  3. // Error reporting:  
  4. error_reporting(E_ALL^E_NOTICE);  
  5.   
  6. include "conn.php";  
  7. include "comment.class.php";  
  8.   
  9.   
  10. /* 
  11. /   Select all the comments and populate the $comments array with objects 
  12. */  
  13.   
  14. $comments = array();  
  15. $result = mysql_query("SELECT * FROM comments ORDER BY id ASC");  
  16.   
  17. while($row = mysql_fetch_assoc($result))  
  18. {  
  19.     $comments[] = new Comment($row);  
  20. }  
  21.   
  22. ?>  
PHP Code
  1. <div id="main">  
  2.   
  3. <?php  
  4.   
  5. /* 
  6. /   Output the comments one by one: 
  7. */  
  8.   
  9. foreach($comments as $c){  
  10.     echo $c->markup();  
  11. }  
  12.   
  13. ?>  
  14.   
  15. <div id="addCommentContainer">  
  16.     <p>Add a Comment</p>  
  17.     <form id="addCommentForm" method="post" action="">  
  18.         <div>  
  19.             <label for="name">Your Name</label>  
  20.             <input type="text" name="name" id="name" />  
  21.               
  22.             <label for="email">Your Email</label>  
  23.             <input type="text" name="email" id="email" />  
  24.               
  25.             <label for="url">Website (not required)</label>  
  26.             <input type="text" name="url" id="url" />  
  27.               
  28.             <label for="body">Comment Body</label>  
  29.             <textarea name="body" id="body" cols="20" rows="5"></textarea>  
  30.               
  31.             <input type="submit" id="submit" value="Submit" />  
  32.         </div>  
  33.     </form>  
  34. </div>  
  35.   
  36. </div>  

 submit.php

PHP Code
  1. <?php  
  2.   
  3. // Error reporting:  
  4. error_reporting(E_ALL^E_NOTICE);  
  5.   
  6. include "conn.php";  
  7. include "comment.class.php";  
  8.   
  9. /* 
  10. /   This array is going to be populated with either 
  11. /   the data that was sent to the script, or the 
  12. /   error messages. 
  13. /*/  
  14.   
  15. $arr = array();  
  16. $validates = Comment::validate($arr);  
  17.   
  18. if($validates)  
  19. {  
  20.     /* Everything is OK, insert to database: */  
  21.       
  22.     mysql_query("   INSERT INTO comments(name,url,email,body) 
  23.                     VALUES ( 
  24.                         '".$arr['name']."',  
  25.                         '".$arr['url']."',  
  26.                         '".$arr['email']."',  
  27.                         '".$arr['body']."'  
  28.                     )");  
  29.       
  30.     $arr['dt'] = date('r',time());  
  31.     $arr['id'] = mysql_insert_id();  
  32.       
  33.     /* 
  34.     /   The data in $arr is escaped for the mysql query, 
  35.     /   but we need the unescaped variables, so we apply, 
  36.     /   stripslashes to all the elements in the array: 
  37.     /*/  
  38.       
  39.     $arr = array_map('stripslashes',$arr);  
  40.       
  41.     $insertedComment = new Comment($arr);  
  42.   
  43.     /* Outputting the markup of the just-inserted comment: */  
  44.   
  45.     echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));  
  46.   
  47. }  
  48. else  
  49. {  
  50.     /* Outputtng the error messages */  
  51.     echo '{"status":0,"errors":'.json_encode($arr).'}';  
  52. }  
  53.   
  54. ?>  

comment.class.php

PHP Code
  1. <?php  
  2.   
  3. class Comment  
  4. {  
  5.     private $data = array();  
  6.       
  7.     public function __construct($row)  
  8.     {  
  9.         /* 
  10.         /   The constructor 
  11.         */  
  12.           
  13.         $this->data = $row;  
  14.     }  
  15.       
  16.     public function markup()  
  17.     {  
  18.         /* 
  19.         /   This method outputs the XHTML markup of the comment 
  20.         */  
  21.           
  22.         // Setting up an alias, so we don't have to write $this->data every time:  
  23.         $d = &$this->data;  
  24.           
  25.         $link_open = ''; 
  26.         $link_close = ''; 
  27.          
  28.         if($d['url']){ 
  29.              
  30.             // If the person has entered a URL when adding a comment, 
  31.             // define opening and closing hyperlink tags 
  32.              
  33.             $link_open = '<a href="'.$d['url'].'">'; 
  34.             $link_close =  '</a>'; 
  35.         } 
  36.          
  37.         // Converting the time to a UNIX timestamp: 
  38.         $d['dt'] = strtotime($d['dt']); 
  39.          
  40.         // Needed for the default gravatar image: 
  41.         $url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';  
  42.           
  43.         return '  
  44.           
  45.             <div class="comment">  
  46.                 <div class="avatar">  
  47.                     '.$link_open.'  
  48.                     <img src="" />  
  49.                     '.$link_close.'  
  50.                 </div>  
  51.                   
  52.                 <div class="name">'.$link_open.$d['name'].$link_close.'</div>  
  53.                 <div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>  
  54.                 <p>'.$d['body'].'</p>  
  55.             </div>  
  56.         '; 
  57.     } 
  58.      
  59.     public static function validate(&$arr) 
  60.     { 
  61.         /* 
  62.         /   This method is used to validate the data sent via AJAX. 
  63.         / 
  64.         /   It return true/false depending on whether the data is valid, and populates 
  65.         /   the $arr array passed as a paremter (notice the ampersand above) with 
  66.         /   either the valid input data, or the error messages. 
  67.         */ 
  68.          
  69.         $errors = array(); 
  70.         $data   = array(); 
  71.          
  72.         // Using the filter_input function introduced in PHP 5.2.0 
  73.          
  74.         if(!($data['email'] = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL))) 
  75.         { 
  76.             $errors['email'] = 'Please enter a valid Email.'; 
  77.         } 
  78.          
  79.         if(!($data['url'] = filter_input(INPUT_POST,'url',FILTER_VALIDATE_URL))) 
  80.         { 
  81.             // If the URL field was not populated with a valid URL, 
  82.             // act as if no URL was entered at all: 
  83.              
  84.             $url = ''; 
  85.         } 
  86.          
  87.         // Using the filter with a custom callback function: 
  88.          
  89.         if(!($data['body'] = filter_input(INPUT_POST,'body',FILTER_CALLBACK,array('options'=>'Comment::validate_text')))) 
  90.         { 
  91.             $errors['body'] = 'Please enter a comment body.'; 
  92.         } 
  93.          
  94.         if(!($data['name'] = filter_input(INPUT_POST,'name',FILTER_CALLBACK,array('options'=>'Comment::validate_text')))) 
  95.         { 
  96.             $errors['name'] = 'Please enter a name.'; 
  97.         } 
  98.          
  99.         if(!empty($errors)){ 
  100.              
  101.             // If there are errors, copy the $errors array to $arr: 
  102.              
  103.             $arr = $errors; 
  104.             return false; 
  105.         } 
  106.          
  107.         // If the data is valid, sanitize all the data and copy it to $arr: 
  108.          
  109.         foreach($data as $k=>$v){ 
  110.             $arr[$k] = mysql_real_escape_string($v); 
  111.         } 
  112.          
  113.         // Ensure that the email is lower case: 
  114.          
  115.         $arr['email'] = strtolower(trim($arr['email'])); 
  116.          
  117.         return true; 
  118.          
  119.     } 
  120.  
  121.     private static function validate_text($str) 
  122.     { 
  123.         /* 
  124.         /   This method is used internally as a FILTER_CALLBACK 
  125.         */ 
  126.          
  127.         if(mb_strlen($str,'utf8')<1) 
  128.             return false; 
  129.          
  130.         // Encode all html special characters (<, >, ", & .. etc) and convert 
  131.         // the new line characters to <br> tags: 
  132.          
  133.         $str = nl2br(htmlspecialchars($str)); 
  134.          
  135.         // Remove the new line characters that are left 
  136.         $str = str_replace(array(chr(10),chr(13)),'',$str);  
  137.           
  138.         return $str;  
  139.     }  
  140.   
  141. }  
  142.   
  143. ?>  

 


原文地址:http://www.freejs.net/article_biaodan_70.html

0
0
分享到:
评论

相关推荐

    简单的ajax评论完整代码.docx

    简单的ajax评论完整代码.docx简单的ajax评论完整代码.docx简单的ajax评论完整代码.docx

    基于AJAX的.NET个人博客系统(完整源码及文档)

    AJAX显示文章列表、AJAX评论、AJAX留言、突出热门显示最新文章、可以划分无限个文章种类、可以制作多个友情链接、评论留言AJAX分页显示、提供文章和评论的RSS源、全局过滤器防SQL注入、后台管理等等。本系统界面友好...

    通用的Ajax评论系统.rar

    代码简单,较为通用,容易整合到其他系统中, 带有评论支持和反对的投票, 可以引用评论,实现盖楼功能, 简单的验证,有验证码防广告功能. 允许程序在 register_globals = off 的环境下工作 安装:将sql.txt导入到...

    jquery+ajax无刷新评论源码(包含无刷新分页)

    www.sendawangluo.com页面获取评论时显示loading加载效果jquery真的是一个非常优秀的JS库,简单容易掌握,对于网页中的多级菜单、级联效果、Tab选项卡切换、图片轮转显示,实现起来都非常的简单,往往就是几句代码的...

    jquery+ajax无刷新评论源码

    实现起来都非常的简单,往往就是几句代码的事。 做AJAX应用,jquery提供的$.get()、$.post()函数都可以用于提交数据,但建议使用$.ajax()来提交,那两个函数都不 提供错误返回信息,不利全面掌控。 提交数据是...

    采用ASP+AJAX+ACCESS数据库编写的留言本网页设计代码

    2、简单的代码,方便用户修改。 3、采用了DIV+CSS的实现,页面更简洁,显示速度更快。 系统具有以下功能: 1、普通用户留言功能。 2、管理员删除留言功能。 3、管理员评论留言功能。 4、留言本的验证码功能,防止...

    IW12.2.8JQueryAjax简单例子

    IWEDIT1的事件处理程序启动AJAX调用,将在IWEDIT1输入的文字传到后台,后台的TIWURLResponderEvent响应前台传来的数据,处理完毕后返回前台,通过前台在AJAX调用时注册的回调函数将处理结果显示在屏幕上。...

    WordPress中利用AJAX技术进行评论提交的实现示例

    谈到 WordPress Ajax 就不得不谈到评论 Ajax提交,作为一个博客、论坛评论的 Ajax 提交不仅可以改善用户体验,还可以大幅缩减服务器开支,毕竟输出单条评论内容比重新组织输出一个页面要简单的多。 虽说现在访问量...

    Ajax 四级导航菜单ASP+Access动态版

    Asp+Ajax无限级联动下拉框菜单Access版 ASP 树形菜单TreeView 多样式版 Ajax仿iGoogle双击编辑、网页拖动完整实例 ASP+jQuery无刷新读写数据库操作 Ajax提交数据实例_Ajax+ASP简单留言本 Ajax仿Google搜索提示ASP+...

    jQuery+ajax简单实现文件上传的方法

    本文实例讲述了jQuery+ajax简单实现文件上传的方法。分享给大家供大家参考,具体如下: 可以通过ajax来提交表单,而不会刷新页面。主要使用的方法是 $(“#formID”).ajaxSubmit()方法。 1、要引入js插件 需要下载的...

    Ajax评论系统-php源码下载-支持盖楼功能

    代码简单,较为通用,容易整合到其他系统中, 带有评论支持和反对的投票, 可以引用评论,实现盖楼功能, 简单的验证,有验证码防广告功能. 安装:将sql.txt导入到mysql修改MooPHP/MooConfig.php的配置即可

    Asp.net利用JQuery AJAX实现无刷新评论思路与代码

    Html页面代码就这样简单就行了: 代码如下: &lt;body&gt;”room”&gt; &lt;/table&gt; &lt;div&gt; 用户名:&lt;input id=”Text1″ type=”text” /&gt; 信息:&lt;textarea id=”TextArea1″ cols=”20″ name=”S1″ rows=”...

    南充海涛AJAX文章系统 v1.0.rar

    南充海涛AJAX文章系统,风格美观大方,功能简约而又实用并以AJAX应用,适用于任何文章、新闻、图片站,代码简单,注释清楚,二次开发和置于其它系统非常方便。 文章显示方式以滑动文章内容方式,简单直观富有灵活感...

    南充海涛PHP AJAX文章系统 v1.0.rar

    南充海涛AJAX文章系统,风格美观大方,功能简约而又实用并以AJAX应用,适用于任何文章、新闻、图片站,代码简单,注释清楚,二次开发和置于其它系统非常方便。  文章显示方式以滑动文章内容方式,简单直观富有灵活...

    仿淘宝星星评论打分样式,可ajax同步到数据库

    代码简单明了,一看就懂 绝对好用 可将评分结果ajax同步到数据库

    通用的Ajax评论系统

    代码简单,较为通用,容易整合到其他系统中, 带有评论支持和反对的投票, 可以引用评论,实现盖楼功能, 简单的验证,有验证码防广告功能. 允许程序在 register_globals = off 的环境下工作 安装:将sql.txt导入到...

    Ajax实战(e文)

    Ajax领域的新框架和组件库层出不穷,一些功能非常简单,一些则是过度的设计或者存在着严重的设计问题。Ajax开发者对于应该选择什么样的框架感到茫然无助,毫无疑问,Ajax in Action可以帮助你。本书是目前已经出版的...

    java+mysql实现的代码分享网(所有源码已开源,效果可看网址:www.admintwo.com)

    8、代码中心,分为了分享代码、下载代码、评论代码、收藏代码。 9、设置功能,支持修改昵称、城市、性别、座右铭、密码、头像。 10、赞助兑换功能,支持1个赞助兑换10个积分,也支持用赞助升级称号。 11、其他功能...

    asp.net mvc2 示例代码

    很简单的一个文章管理示例代码,在这个项目中使用了一些asp.net的新特性,例如数据验证功能以及强类型的HtmlHelper等,功能方面只实现了文章列表和添加文章的功能,至于编辑和删除功能实在太简单了,就没有实现,...

    struts2 + jsp + ajax + jquery + hibernate + mysql通讯录

    都是一些基础的技术,像登录,注册验证用到了ajax和jquery结合,代码更加简单易懂。 后台用了hibernate技术,数据库用了mysql。 页面主要有登录,注册,显示联系人列表的主页面,修改,增加,删除,查找联系人等功能...

Global site tag (gtag.js) - Google Analytics