本文就是和大家分享一款由php結合ajax實現的無重新整理留言板,先給大家看一下最後的效果圖:
資料庫連接代碼如下:
<?php$conn = @mysql_connect("localhost","root","root") or die ("MySql串連錯誤");mysql_select_db("demo",$conn);mysql_query("set names 'utf8'");?>
index.php檔案代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><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" type="text/javascript"></script></head><body><h1>無重新整理顯示回帖</h1><div id="thread"><?phpinclude("conn.php");$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<?php echo $row['id'];?>"> <div class="post_title"><?php echo $row['title'];?> [<?php echo $row['username'];?>]</div> <div class="post_content"><pre><?php echo $row['content'];?></pre></div> </div><?php}?></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檔案代碼如下
<?phpinclude("conn.php");$title = $_POST["title"]; //擷取title$content = $_POST["content"]; //擷取content$username = $_POST["username"]; //擷取username$threadId = $_POST["threadid"]; //擷取threadid$sql="insert into bbs_post (title,content,username,threadid) " . "values ('$title','$content','$username','$threadId')"; 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; if (checkForm()) { createXmlHttp(); //建立XMLHttpRequest對象 xmlHttp.onreadystatechange = submitPostCallBack; //設定回呼函數 xmlHttp.open("POST", "bbspost.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建立新的divfunction createDiv(className, text) { var newDiv = document.createElement("div"); newDiv.className = className; newDiv.innerHTML = text; return newDiv;}
bbs.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;}
以上就是本文的全部內容,希望對大家的學習有所協助。