Table of Contents
无刷新显示回帖
Home Backend Development PHP Tutorial php+ajax制作无刷新留言板_php实例

php+ajax制作无刷新留言板_php实例

Jun 07, 2016 pm 05:11 PM
php php message board

本文就是和大家分享一款由php结合ajax实现的无刷新留言板,先给大家看一下最后的效果图:

数据库连接代码如下:

<&#63;php
$conn = @mysql_connect("localhost","root","root") or die ("MySql连接错误");
mysql_select_db("demo",$conn);
mysql_query("set names 'utf8'");
&#63;>
Copy after login

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 id="无刷新显示回帖">无刷新显示回帖</h1>
<div id="thread">
<&#63;php
include("conn.php");
$sql = "select * from `bbs_post` where `threadid` ='1' order by id asc";
$query =mysql_query($sql);
while($row = mysql_fetch_array($query)){ 
&#63;>
 <div class="post" id="post<&#63;php echo $row['id'];&#63;>">
    <div class="post_title"><&#63;php echo $row['title'];&#63;> [<&#63;php echo $row['username'];&#63;>]</div>
    <div class="post_content"><pre class="brush:php;toolbar:false"><&#63;php echo $row['content'];&#63;>
Copy after login
<?php } ?>
回帖
姓名:
标题:
内容:

bbspost.php文件代码如下

<&#63;php
include("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;
&#63;>

Copy after login

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 class="brush:php;toolbar:false">" + content + "
Copy after login
"); //创建内容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; }

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;
}
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles