网站地图    收藏   

主页 > php专栏 > php综合实列 >

php ajax 留言板 - 综合实例

来源:自学PHP网    时间:2014-12-02 13:09 作者: 阅读:次

[导读] 提供一款国人写的留言板,他是利用了jquery php mysql ajax来实现php ajax 局部刷新留方板实例的喜欢就下载吧,代码如下:$link=@mysql_connect($db_host,$db_user,$db_pass)ordie(#39;unableto......

php ajax 留言板

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

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

数据库结构,代码如下:

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

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论