ajax+php无刷新回贴和注册检验实例
先看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"];
- $content = $_POST["content"];
- $username = $_POST["username"];
- $threadId = $_POST["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);
-
- $responseId=mysql_insert_id();
- echo $responseId;
- ?>
bbs.js文件,里面包括了大量ajax文件,代码如下:
-
- var xmlHttp;
- var username;
- var title;
- var content;
- var threadid;
-
-
- function createXmlHttp() {
-
- if (window.XMLHttpRequest) {
- xmlHttp = new XMLHttpRequest();
- } else {
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
- }
- }
-
-
- 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();
- xmlHttp.onreadystatechange = submitPostCallBack;
- xmlHttp.open("POST", "bbs_post.php", true);
-
- 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", "");
- postDiv.id = "post" + postId;
-
- var postTitleDiv = createDiv("post_title", title + " [" + username + "]");
- var postContentDiv = createDiv("post_content", "<pre>" + content + "</pre>");
- postDiv.appendChild(postTitleDiv);
- postDiv.appendChild(postContentDiv);
-
- document.getElementById("thread").appendChild(postDiv);
- }
-
-
- 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;
- }
-
-
- #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;
- }
-
-
- div.post_title {
- border-bottom:1px dotted #0066CC;
- font-weight:bold;
- }
-
-
- 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;
- }