先看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;
- }
新聞熱點
疑難解答