当前位置:首页 > 综合实例 > 列表

php+ajax制作无刷新留言板

发布:smiling 来源: PHP粉丝网  添加日期:2021-06-23 18:32:32 浏览: 评论:0 

这篇文章主要介绍了一款基于php+ajax实现无刷新留言板实例,需要的朋友可以参考下,本文就是和大家分享一款由php结合ajax实现的无刷新留言板。

数据库连接代码如下:

  1. <?php 
  2. $conn = @mysql_connect("localhost","root","root") or die ("MySql连接错误"); 
  3. mysql_select_db("demo",$conn); 
  4. mysql_query("set names 'utf8'"); 
  5. ?> 

index.php文件代码如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
  5. <link href="bbs.css" type="text/css" rel="stylesheet"> 
  6. <title>无刷新显示回帖</title> 
  7. <script src="bbs.js" type="text/javascript"></script> 
  8. </head> 
  9.  
  10. <body> 
  11. <h1>无刷新显示回帖</h1> 
  12. <div id="thread"> 
  13. <?php 
  14. include("conn.php"); 
  15. $sql = "select * from `bbs_post` where `threadid` ='1' order by id asc"; 
  16. $query =mysql_query($sql); 
  17. while($row = mysql_fetch_array($query)){  
  18. ?> 
  19.  <div class="post" id="post<?php echo $row['id'];?>"> 
  20.     <div class="post_title"><?php echo $row['title'];?> [<?php echo $row['username'];?>]</div> 
  21.     <div class="post_content"><pre><?php echo $row['content'];?></pre></div> 
  22.    </div> 
  23. <?php 
  24. } 
  25. ?> 
  26. </div> 
  27.  
  28. <table class="reply"> 
  29. <tr> 
  30.  <td colspan="2" class="title">回帖<input type="hidden" name="threadid" id="threadid" value="1"></td> 
  31. </tr> 
  32. <tr> 
  33.  <td>姓名:</td> 
  34.  <td><input type="text" name="username" id="username"></td> 
  35. </tr> 
  36. <tr> 
  37.  <td>标题:</td> 
  38.  <td><input type="text" name="post_title" id="post_title"></td> 
  39. </tr> 
  40. <tr> 
  41.  <td>内容:</td> 
  42.  <td><textarea name="post_content" id="post_content"></textarea></td> 
  43. </tr> 
  44. <tr> 
  45.  <td colspan="2"><input type="button" onclick="submitPost()" value="提交"></td> 
  46. </tr> 
  47. </table> 
  48. </body> 
  49. </html> 

bbspost.php文件代码如下

  1. <?php 
  2. include("conn.php"); 
  3. $title = $_POST["title"]; //获取title 
  4. $content = $_POST["content"]; //获取content 
  5. $username = $_POST["username"]; //获取username 
  6. $threadId = $_POST["threadid"]; //获取threadid 
  7.  
  8.  
  9. $sql="insert into bbs_post (title,content,username,threadid) " . 
  10.  "values ('$title','$content','$username','$threadId')"; 
  11.  mysql_query($sql); 
  12.  //传回最后一次使用 INSERT 指令的 ID 
  13. $responseId=mysql_insert_id(); 
  14. echo $responseId; 
  15. ?> 

bbs.js文件里面包括了大量ajax文件,代码如下:

  1. //先创建一个空的bbs.js文件,并修改其属性为utf-8,才能保存中文。 
  2. var xmlHttp;      //用于保存XMLHttpRequest对象的全局变量 
  3. var username;      //用于保存姓名 
  4. var title;       //用于保存标题 
  5. var content;      //用于保存内容 
  6. var threadid;      //用于保存主题编号 
  7.  
  8. //用于创建XMLHttpRequest对象 
  9. function createXmlHttp() { 
  10.  //根据window.XMLHttpRequest对象是否存在使用不同的创建方式 
  11.  if (window.XMLHttpRequest) { 
  12.   xmlHttp = new XMLHttpRequest();     //FireFox、Opera等浏览器支持的创建方式 
  13.  } else { 
  14.   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器支持的创建方式 
  15.  } 
  16. } 
  17.  
  18. //提交回帖到服务器 
  19. function submitPost() { 
  20.  //获取帖子中姓名、标题、内容、主题编号四部分信息 
  21.  username = document.getElementById("username").value; 
  22.  title = document.getElementById("post_title").value; 
  23.  content = document.getElementById("post_content").value; 
  24.  threadid = document.getElementById("threadid").value; 
  25.  if (checkForm()) { 
  26.   createXmlHttp();         //创建XMLHttpRequest对象 
  27.   xmlHttp.onreadystatechange = submitPostCallBack; //设置回调函数 
  28.   xmlHttp.open("POST", "bbspost.php", true);   //发送POST请求 
  29.   //设置POST请求体类型 
  30.   xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
  31.   xmlHttp.send("username=" + encodeURI(username) + 
  32.       "&title=" + encodeURI(title) + 
  33.       "&content=" + encodeURI(content) + 
  34.       "&threadid=" + threadid);    //发送包含四个参数的请求体 
  35.  } 
  36. } 
  37.  
  38. //检查表单是否内容已填写完毕 
  39. function checkForm() { 
  40.  if (username == "") { 
  41.   alert("请填写姓名"); 
  42.   return false; 
  43.  } else if (title == "") { 
  44.   alert("请填写标题"); 
  45.   return false; 
  46.  } else if (content == "") { 
  47.   alert("请填写内容"); 
  48.   return false; 
  49.  } 
  50.  return true; 
  51. } 
  52.  
  53. //获取查询选项的回调函数 
  54. function submitPostCallBack() { 
  55.  if (xmlHttp.readyState == 4) { 
  56. alert(xmlHttp.responseText); 
  57.   createNewPost(xmlHttp.responseText); 
  58.  } 
  59. } 
  60.  
  61. //创建新的回帖 
  62. function createNewPost(postId) { 
  63.  //清空当前表单中各部分信息 
  64.  document.getElementById("post_title").value = ""; 
  65.  document.getElementById("post_content").value = ""; 
  66.  document.getElementById("username").value = ""; 
  67.  
  68.  var postDiv = createDiv("post", ""); //创建回帖的外层div 
  69.  postDiv.id = "post" + postId;   //给新div赋id值 
  70.  
  71.  var postTitleDiv = createDiv("post_title", title + " [" + username + "]"); //创建标题div 
  72.  var postContentDiv = createDiv("post_content", "<pre>" + content + "</pre>"); //创建内容div 
  73.  postDiv.appendChild(postTitleDiv);       //在外层div追加标题 
  74.  postDiv.appendChild(postContentDiv);      //在外层div追加内容 
  75.  
  76.  document.getElementById("thread").appendChild(postDiv);  //将外层div追加到主题div中 
  77. } 
  78.  
  79. //根据className和text创建新的div 
  80. function createDiv(className, text) { 
  81.  var newDiv = document.createElement("div"); 
  82.  newDiv.className = className; 
  83.  newDiv.innerHTML = text; 
  84.  return newDiv; 
  85. }  

bbs.css文件如下:

  1. /* 页面基本样式 */ 
  2. body, td, input, textarea { 
  3.  font-family:Arial; 
  4.  font-size:12px; 
  5. } 
  6.  
  7. /* 主题的样式 */ 
  8. #thread { 
  9.  border:1px solid black; 
  10.  width:300px; 
  11.  margin-bottom:10px; 
  12. } 
  13.  
  14. /* 提示信息div的样式 */ 
  15. #statusDiv { 
  16.  border:1px solid #999; 
  17.  background:#FFFFCC; 
  18.  width:100px; 
  19.  position:absolute; 
  20.  top:50%; 
  21.  left:50%; 
  22.  margin:-50px 0 0 -100px; 
  23.  width:280px; 
  24. } 
  25.  
  26. /* 帖子的样式 */ 
  27. div.post { 
  28.  border-bottom:1px solid black; 
  29.  padding:5px; 
  30. } 
  31.  
  32. /* 帖子title的样式 */ 
  33. div.post_title { 
  34.  border-bottom:1px dotted #0066CC; 
  35.  font-weight:bold; 
  36. } 
  37.  
  38. /* 帖子content的样式 */ 
  39. div.post_content { 
  40.  font-size:12px; 
  41.  margin:5px; 
  42. } 
  43.  
  44. /* 回帖表格基本样式 */ 
  45. table.reply { 
  46.  border-collapse:collapse; 
  47.  width:300px; 
  48. } 
  49.  
  50. /* 回帖表格单元格样式 */ 
  51. table.reply td { 
  52.  border:1px solid black; 
  53.  padding:3px; 
  54. } 
  55.  
  56. /* 回帖表格表头样式 */ 
  57. table.reply td.title { 
  58.  background:#003366; 
  59.  color:#FFFFFF; 
  60. } 
  61.  
  62. /* 表单元素样式 */ 
  63. input, textarea { 
  64.  border:1px solid black; 
  65. } 
  66.  
  67. /* 文字区域样式 */ 
  68. textarea { 
  69.  width:200px; 
  70.  height:50px; 
  71. } 
  72.  
  73. /* 预定义格式样式 */ 
  74. pre { 
  75.  margin:0; 
  76. }

Tags: php+ajax无刷新留言板

分享到: