读书人

容易的ajax评论完整代码

发布时间: 2013-11-02 19:41:10 作者: rapoo

简单的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;容易的ajax评论完整代码演示

?

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

读书人网 >Ajax

热点推荐