国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

ajax+php無刷新回貼和注冊檢驗實例

2024-05-04 23:06:24
字體:
來源:轉載
供稿:網友

先看xin.sql數據庫,我們可直接復制保存成xxx.sql,代碼如下:

  1. use xin; 
  2. CREATE TABLE bbs_post ( 
  3.   id int(11) NOT NULL auto_increment, 
  4.   title varchar(255) NOT NULL
  5.   username varchar(255) NOT NULL
  6.   content varchar(255) NOT NULL
  7.   threadid int(11) NOT NULL
  8.   PRIMARY KEY  (id) 
  9. ); 
  10.  
  11. INSERT INTO bbs_post VALUES (1,'大家了解Ajax技術嗎?','ajaxuser','如何學習Ajax技術呢?',1),(2,'通過實例學習應該不錯','tom','先看看基礎概念,然后多從實例中學習。',1),(3,'謝謝!','max','非常感謝你的建議!',1); 

index.php文件,代碼如下:

  1. <html> 
  2. <head> 
  3. <meta http-equiv="Content-type" content="text/html; charset=utf-8"
  4. <link href="bbs.css" type="text/css" rel="stylesheet"
  5. <title>無刷新顯示回帖</title> 
  6. <script src="bbs.js"></script> 
  7. </head> 
  8.  
  9. <body> 
  10. <h1>無刷新顯示回帖</h1> 
  11. <div id="thread"
  12. <? 
  13.  $conn = @mysql_connect("localhost","root","123"or die ("MySql連接錯誤"); 
  14.  mysql_select_db("xin",$conn); 
  15.  mysql_query("set names 'utf8'"); 
  16.  $sql = "select * from bbs_post where threadid = 1 order by id asc"
  17.  $query = mysql_query($sql); 
  18.  while($row = mysql_fetch_array($query)){ 
  19. ?> 
  20.    <div class="post" id="post<?=$row[id]?>"
  21.                 <div class="post_title"><?=$row[title]?> [<?=$row[username]?>]</div> 
  22.                 <div class="post_content"><pre><?=$row[content]?></pre></div> 
  23.          </div> 
  24. <? 
  25.  } 
  26. ?> 
  27. </div> 
  28.  
  29. <table class="reply"
  30. <tr> 
  31.     <td colspan="2" class="title">回帖<input type="hidden" name="threadid" id="threadid" value="1"></td> 
  32. </tr> 
  33. <tr> 
  34.     <td>姓名:</td> 
  35.     <td><input type="text" name="username" id="username"></td> 
  36. </tr> 
  37. <tr> 
  38.     <td>標題:</td> 
  39.     <td><input type="text" name="post_title" id="post_title"></td> 
  40. </tr> 
  41. <tr> 
  42.     <td>內容:</td> 
  43.     <td><textarea name="post_content" id="post_content"></textarea></td> 
  44. </tr> 
  45. <tr> 
  46.     <td colspan="2"><input type="button" onclick="submitPost()" value="提交"></td> 
  47. </tr> 
  48. </table> 
  49. </body> 
  50. </html> 

bbspost.php文件,代碼如下:

  1. <?php 
  2. $title = $_POST["title"]; //獲取title 
  3. $content = $_POST["content"]; //獲取content 
  4. $username = $_POST["username"]; //獲取username 
  5. $threadId = $_POST["threadid"]; //獲取threadid 
  6.  
  7. $conn = @ mysql_connect("localhost""root""123"or die("MySql連接錯誤"); 
  8. mysql_select_db("xin"$conn); 
  9. mysql_query("set names 'utf8'"); 
  10.  
  11. $sql="insert into bbs_post (title,content,username,threadid) " . 
  12.     "values ('$title','$content','$username','$threadId')"
  13.    echo $sql
  14.    mysql_query($sql); 
  15.   //傳回最后一次使用 INSERT 指令的 ID 
  16.  $responseId=mysql_insert_id(); 
  17.  echo $responseId
  18. ?> 

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. function submitPost() { 
  19.     //獲取帖子中姓名、標題、內容、主題編號四部分信息 
  20.     username = document.getElementById("username").value; 
  21.     title = document.getElementById("post_title").value; 
  22.     content = document.getElementById("post_content").value; 
  23.     threadid = document.getElementById("threadid").value; 
  24.  alert(username+""+title+""+content+""+threadid); 
  25.     if (checkForm()) { 
  26.         createXmlHttp();                                    //創建XMLHttpRequest對象 
  27.         xmlHttp.onreadystatechange = submitPostCallBack;    //設置回調函數 
  28.         xmlHttp.open("POST""bbs_post.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. function checkForm() { 
  39.     if (username == "") { 
  40.         alert("請填寫姓名"); 
  41.         return false; 
  42.     } else if (title == "") { 
  43.         alert("請填寫標題"); 
  44.         return false; 
  45.     } else if (content == "") { 
  46.         alert("請填寫內容"); 
  47.         return false; 
  48.     } 
  49.     return true; 
  50.  
  51. //獲取查詢選項的回調函數 
  52. function submitPostCallBack() { 
  53.     if (xmlHttp.readyState == 4) { 
  54.      alert(xmlHttp.responseText); 
  55.         createNewPost(xmlHttp.responseText); 
  56.     } 
  57.  
  58. //創建新的回帖 
  59. function createNewPost(postId) { 
  60.     //清空當前表單中各部分信息 
  61.     document.getElementById("post_title").value = ""
  62.     document.getElementById("post_content").value = ""
  63.     document.getElementById("username").value = ""
  64.  
  65.     var postDiv = createDiv("post""");    //創建回帖的外層div 
  66.     postDiv.id = "post" + postId;           //給新div賦id值 
  67.  
  68.     var postTitleDiv = createDiv("post_title", title + " [" + username + "]");  //創建標題div 
  69.     var postContentDiv = createDiv("post_content""<pre>" + content + "</pre>");    //創建內容div 
  70.     postDiv.appendChild(postTitleDiv);                          //在外層div追加標題 
  71.     postDiv.appendChild(postContentDiv);                        //在外層div追加內容 
  72.  
  73.     document.getElementById("thread").appendChild(postDiv);     //將外層div追加到主題div中 
  74.  
  75. //根據className和text創建新的div 
  76. function createDiv(className, text) { 
  77.     var newDiv = document.createElement("div"); 
  78.     newDiv.className = className; 
  79.     newDiv.innerHTML = text; 
  80.     return newDiv; 

css文件如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东港市| 锡林浩特市| 通榆县| 丰城市| 南川市| 本溪市| 丰台区| 兴海县| 罗江县| 北宁市| 闽侯县| 正阳县| 兰州市| 石阡县| 岳普湖县| 泰安市| 金门县| 黄骅市| 尼玛县| 吉首市| 冀州市| 溆浦县| 察隅县| 绥化市| 安国市| 句容市| 桐梓县| 舒城县| 林周县| 临潭县| 乐东| 松桃| 汉源县| 镇康县| 海南省| 淮北市| 堆龙德庆县| 兴安县| 和顺县| 沛县| 铜鼓县|