A-A+

php ajax 留言板

2019年02月26日 我爱编程, 未分类 暂无评论

提供一款国人写的留言板,他是利用了jquery php mysql ajax来实现php ajax 局部刷新留方板实例的喜欢就下载吧,代码如下:

  1. $link = @mysql_connect($db_host,$db_user,$db_passor die('unable to establish a db connection');
  2. mysql_query("set names 'utf8'");
  3. mysql_select_db($db_database,$link);
  4. class comment
  5. {
  6.  private $data = array();
  7.  public function __construct($row)
  8.  {
  9.   /*
  10.   / the constructor
  11.   */
  12.   $this->data = $row;
  13.  }
  14.  public function markup()
  15.  {
  16.   /*
  17.   / this method outputs the xhtml markup of the comment
  18.   */
  19.   // setting up an alias, so we don't have to write $this->data every time:
  20.   $d = &$this->data;
  21.   $link_open = '';
  22.   $link_close = '';
  23.   if($d['url']){
  24.    // if the person has entered a url when adding a comment,
  25.    // define opening and closing hyperlink tags
  26.    $link_open = '<a href="'.$d['url'].'">';
  27.    $link_close =  '</a>';
  28.   }
  29.   // converting the time to a unix timestamp:
  30.   $d['dt'] = strtotime($d['dt']);
  31.   // needed for the default gravatar image:
  32.   $url = 'http://'.dirname($_server['server_name'].$_server["request_uri"]).'/img/default_avatar.gif';
  33.   return '
  34.    <div class="comment">
  35.     <div class="avatar">
  36.      '.$link_open.'
  37.      <img src="http://www.gravatar.com/avatar/'.md5($d['email']).'?size=50&amp;default='.urlencode($url).'" />
  38.      '.$link_close.'
  39.     </div>
  40.     <div class="name">'.$link_open.$d['name'].$link_close.'</div>
  41.     <div class="date" title="added at '.date('h:i on d m y',$d['dt']).'">'.date('d m y',$d['dt']).'</div>
  42.     <p>'.$d['body'].'</p>
  43.    </div>
  44.   ';
  45.  }
  46.  public static function validate(&$arr)
  47.  {
  48.   /*
  49.   / this method is used to validate the data sent via ajax.
  50.   /
  51.   / it return true/false depending on whether the data is valid, and populates
  52.   / the $arr array passed as a paremter (notice the ampersand above) with
  53.   / either the valid input data, or the error messages.
  54.   */
  55.   $errors = array();
  56.   $data = array();
  57.   // using the filter_input function introduced in php 5.2.0
  58.   if(!($data['email'] = filter_input(input_post,'email',filter_validate_email)))
  59.   {
  60.    $errors['email'] = 'please enter a valid email.';
  61.   }
  62.   if(!($data['url'] = filter_input(input_post,'url',filter_validate_url)))
  63.   {
  64.    // if the url field was not populated with a valid url,
  65.    // act as if no url was entered at all:
  66.    $url = '';
  67.   }
  68.   // using the filter with a custom callback function:
  69.   if(!($data['body'] = filter_input(input_post,'body',filter_callback,array('options'=>'comment::validate_text'))))
  70.   {
  71.    $errors['body'] = 'please enter a comment body.';
  72.   }
  73.   if(!($data['name'] = filter_input(input_post,'name',filter_callback,array('options'=>'comment::validate_text'))))
  74.   {
  75.    $errors['name'] = 'please enter a name.';
  76.   }
  77.   if(!emptyempty($errors)){
  78.    // if there are errors, copy the $errors array to $arr:
  79.    $arr = $errors;
  80.    return false;
  81.   }
  82.   // if the data is valid, sanitize all the data and copy it to $arr:
  83.   foreach($data as $k=>$v){
  84.    $arr[$k] = mysql_real_escape_string($v);
  85.   }
  86.   // ensure that the email is lower case:
  87.   $arr['email'] = strtolower(trim($arr['email']));
  88.   return true;
  89.  }
  90.  private static function validate_text($str)
  91.  {
  92.   /*
  93.   / this method is used internally as a filter_callback
  94.   */
  95.   if(mb_strlen($str,'utf8')<1)
  96.    return false;
  97.   // encode all html special characters (<, >, ", & .. etc) and convert
  98.   // the new line characters to <br> tags:
  99.   $str = nl2br(htmlspecialchars($str));
  100.   // remove the new line characters that are left
  101.   $str = str_replace(array(chr(10),chr(13)),'',$str);
  102.   return $str;
  103.  }
  104. }
  105. $comments = array();
  106. $result = mysql_query("select * from comments order by id asc");
  107. while($row = mysql_fetch_assoc($result))
  108. {
  109.  $comments[] = new comment($row);
  110. }
  111. ?>
  112. <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">
  113. <html xmlns="http://www.w3.org/1999/xhtml">
  114. <head>
  115. <meta http-equiv="content-type" content="text/html; charset=gb2312" />
  116. <title>simple ajax commenting system | tutorialzine demo</title>
  117. <link rel="stylesheet" type="text/css教程" href="styles.css" />
  118. </head>
  119. <body>
  120. <div id="main">
  121. <?php
  122. /*
  123. / output the comments one by one:
  124. */
  125. foreach($comments as $c){
  126.  echo $c->markup();
  127. }
  128. ?>
  129. <div id="addcommentcontainer">
  130.  <p>add a comment</p>
  131.  <form id="addcommentform" method="post" action="">
  132.      <div>
  133.          <label for="name">your name</label>
  134.          <input type="text" name="name" id="name" />
  135.             <label for="email">your email</label>
  136.             <input type="text" name="email" id="email" />
  137.             <label for="url">website (not required)</label>
  138.             <input type="text" name="url" id="url" />
  139.             <label for="body">comment body</label>
  140.             <textarea name="body" id="body" cols="20" rows="5"></textarea>
  141.             <input type="submit" id="submit" value="submit" />
  142.         </div>
  143.     </form>
  144. </div>
  145. </div>
  146. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  147. <script type="text/javascript" src="script.js"></script>
  148. </body>
  149. </html>

数据库结构,代码如下:

  1. --
  2. -- table structure for table `comments`
  3. --
  4. create table `comments` (
  5.   `id` int(10) unsigned not null auto_increment,
  6.   `namevarchar(128) collate utf8_unicode_ci not null default '',
  7.   `url` varchar(255) collate utf8_unicode_ci not null default '',
  8.   `email` varchar(255) collate utf8_unicode_ci not null default '',
  9.   `body` text collate utf8_unicode_ci not null,
  10.   `dt` timestamp not null default '0000-00-00',
  11.   primary key  (`id`)
  12. ) engine=myisam  default charset=utf8 collate=utf8_unicode_ci;

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录