php ajax 留言板
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-09 10:31:08 浏览: 评论:0
提供一款国人写的留言板,他是利用了jquery php mysql ajax来实现php ajax 局部刷新留方板实例的喜欢就下载吧,代码如下:
- $link = @mysql_connect($db_host,$db_user,$db_pass) or die('unable to establish a db connection');
- mysql_query("set names 'utf8'");
- mysql_select_db($db_database,$link);
- 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="http://www.gravatar.com/avatar/'.md5($d['email']).'?size=50&default='.urlencode($url).'" />
- '.$link_close.'
- </div>
- <div class="name">'.$link_open.$d['name'].$link_close.'</div>
- <div class="date" title="added at '.date('h:i on 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(!emptyempty($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;
- }
- }
- $comments = array();
- $result = mysql_query("select * from comments order by id asc");
- while($row = mysql_fetch_assoc($result))
- {
- $comments[] = new comment($row);
- }
- ?>
- <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=gb2312" />
- <title>simple ajax commenting system | tutorialzine demo</title>
- <link rel="stylesheet" type="text/css教程" href="styles.css" />
- //开源代码phpfensi.com
- </head>
- <body>
- <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>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
- <script type="text/javascript" src="script.js"></script>
- </body>
- </html>
数据库结构,代码如下:
- --
- -- table structure for table `comments`
- --
- 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 '0000-00-00',
- primary key (`id`)
- ) engine=myisam default charset=utf8 collate=utf8_unicode_ci;
Tags: php ajax 留言板
- 上一篇:php投票系统简单实现源码
- 下一篇:php 针探,查看服务器详细信息
相关文章
- ·PHP+TEXT留言本(一)(2013-11-11)
- ·PHP+TEXT留言本(二)(2013-11-11)
- ·PHP+TEXT留言本(三)(2013-11-11)
- ·PHP+TEXT留言本(四)(2013-11-11)
- ·PHP+TEXT留言本(五)(2013-11-11)
- ·PHP+TEXT留言本(六)(2013-11-11)
- ·PHP+MYSQL留言本(一)(2013-11-16)
- ·PHP+MYSQL留言本(二)(2013-11-16)
- ·PHP+MYSQL留言本(三)(2013-11-16)
- ·PHP设计聊天室步步通(一)(2013-11-16)
- ·PHP设计聊天室步步通(二) (2013-11-16)
- ·PHP设计聊天室步步通(三) (2013-11-16)
- ·PHP设计聊天室步步通(四) (2013-11-16)
- ·php注册页面代码(mysql+php)(2013-12-11)
- ·php 用户注册页面代码(2013-12-11)
- ·php写的网页计数器代码(2013-12-11)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)