先看xin.sql資料庫,我們可直接複製儲存成xxx.sql哦。
| 代碼如下 |
複製代碼 |
use xin; CREATE TABLE bbs_post ( id int(11) NOT NULL auto_increment, title varchar(255) NOT NULL, username varchar(255) NOT NULL, content varchar(255) NOT NULL, threadid int(11) NOT NULL, PRIMARY KEY (id) ); |
INSERT INTO bbs_post VALUES (1,'大家瞭解Ajax技術嗎?','ajaxuser','如何學習Ajax技術呢?',1),(2,'通過執行個體學習應該不錯','tom','先看看基礎概念,然後多從執行個體中學習。',1),(3,'謝謝!','max','非常感謝你的建議!',1);
index.php檔案
| 代碼如下 |
複製代碼 |
<html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <link href="bbs.css" type="text/css" rel="stylesheet"> <title>無重新整理顯示回帖</title> <script src="bbs.js"></script> </head> <body> <h1>無重新整理顯示回帖</h1> <div id="thread"> <? $conn = @mysql_connect("localhost","root","123") or die ("MySql串連錯誤"); mysql_select_db("xin",$conn); mysql_query("set names 'utf8'"); $sql = "select * from bbs_post where threadid = 1 order by id asc"; $query = mysql_query($sql); while($row = mysql_fetch_array($query)){ ?> <div class="post" id="post<?=$row[id]?>"> <div class="post_title"><?=$row[title]?> [<?=$row[username]?>]</div> <div class="post_content"><pre><?=$row[content]?></pre></div> </div> <? } ?> </div> <table class="reply"> <tr> <td colspan="2" class="title">回帖<input type="hidden" name="threadid" id="threadid" value="1"></td> </tr> <tr> <td>姓名:</td> <td><input type="text" name="username" id="username"></td> </tr> <tr> <td>標題:</td> <td><input type="text" name="post_title" id="post_title"></td> </tr> <tr> <td>內容:</td> <td><textarea name="post_content" id="post_content"></textarea></td> </tr> <tr> <td colspan="2"><input type="button" onclick="submitPost()" value="提交"></td> </tr> </table> </body> </html> |
bbspost.php檔案
| 代碼如下 |
複製代碼 |
<?php $title = $_POST["title"]; //擷取title $content = $_POST["content"]; //擷取content $username = $_POST["username"]; //擷取username $threadId = $_POST["threadid"]; //擷取threadid $conn = @ mysql_connect("localhost", "root", "123") or die("MySql串連錯誤"); mysql_select_db("xin", $conn); mysql_query("set names 'utf8'"); $sql="insert into bbs_post (title,content,username,threadid) " . "values ('$title','$content','$username','$threadId')"; echo $sql; mysql_query($sql); //傳回最後一次使用 INSERT 指令的 ID $responseId=mysql_insert_id(); echo $responseId; ?>
|
bbs.js檔案,裡麵包括了大量ajax檔案啊
| 代碼如下 |
複製代碼 |
//先建立一個空的bbs.js檔案,並修改其屬性為utf-8,才能儲存中文。 var xmlHttp; //用於儲存XMLHttpRequest對象的全域變數 var username; //用於儲存姓名 var title; //用於儲存標題 var content; //用於儲存內容 var threadid; //用於儲存主題編號 //用於建立XMLHttpRequest對象 function createXmlHttp() { //根據window.XMLHttpRequest對象是否存在使用不同的建立方式 if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); //FireFox、Opera等瀏覽器支援的建立方式 } else { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE瀏覽器支援的建立方式 } } //提交回帖到伺服器 function submitPost() { //擷取文章中姓名、標題、內容、主題編號四部分資訊 username = document.getElementById("username").value; title = document.getElementById("post_title").value; content = document.getElementById("post_content").value; threadid = document.getElementById("threadid").value; alert(username+""+title+""+content+""+threadid); if (checkForm()) { createXmlHttp(); //建立XMLHttpRequest對象 xmlHttp.onreadystatechange = submitPostCallBack; //設定回呼函數 xmlHttp.open("POST", "bbs_post.php", true); //發送POST請求 //設定POST請求體類型 xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlHttp.send("username=" + encodeURI(username) + "&title=" + encodeURI(title) + "&content=" + encodeURI(content) + "&threadid=" + threadid); //發送包含四個參數的請求體 } } //檢查表單是否內容已填寫完畢 function checkForm() { if (username == "") { alert("請填寫姓名"); return false; } else if (title == "") { alert("請填寫標題"); return false; } else if (content == "") { alert("請填寫內容"); return false; } return true; } //擷取查詢選項的回呼函數 function submitPostCallBack() { if (xmlHttp.readyState == 4) { alert(xmlHttp.responseText); createNewPost(xmlHttp.responseText); } } //建立新的回帖 function createNewPost(postId) { //清空當前表單中各部分資訊 document.getElementById("post_title").value = ""; document.getElementById("post_content").value = ""; document.getElementById("username").value = ""; var postDiv = createDiv("post", ""); //建立回帖的外層div postDiv.id = "post" + postId; //給新div賦id值 var postTitleDiv = createDiv("post_title", title + " [" + username + "]"); //建立標題div var postContentDiv = createDiv("post_content", "<pre>" + content + "</pre>"); //建立內容div postDiv.appendChild(postTitleDiv); //在外層div追加標題 postDiv.appendChild(postContentDiv); //在外層div追加內容 document.getElementById("thread").appendChild(postDiv); //將外層div追加到主題div中 } //根據className和text建立新的div function createDiv(className, text) { var newDiv = document.createElement("div"); newDiv.className = className; newDiv.innerHTML = text; return newDiv; } |
css檔案如下
| 代碼如下 |
複製代碼 |
/* 頁面基本樣式 */ body, td, input, textarea { font-family:Arial; font-size:12px; } /* 主題的樣式 */ #thread { border:1px solid black; width:300px; margin-bottom:10px; } /* 提示資訊div的樣式 */ #statusDiv { border:1px solid #999; background:#FFFFCC; width:100px; position:absolute; top:50%; left:50%; margin:-50px 0 0 -100px; width:280px; } /* 文章的樣式 */ div.post { border-bottom:1px solid black; padding:5px; } /* 文章title的樣式 */ div.post_title { border-bottom:1px dotted #0066CC; font-weight:bold; } /* 文章content的樣式 */ div.post_content { font-size:12px; margin:5px; } /* 回帖表格基本樣式 */ table.reply { border-collapse:collapse; width:300px; } /* 回帖表格儲存格樣式 */ table.reply td { border:1px solid black; padding:3px; } /* 回帖表格表頭樣式 */ table.reply td.title { background:#003366; color:#FFFFFF; } /* 表單元素樣式 */ input, textarea { border:1px solid black; } /* 文字地區樣式 */ textarea { width:200px; height:50px; } /* 預定義格式樣式 */ pre { margin:0; } |