Php+ajax make no refresh message board, phpajax Refresh message Board _php tutorial

Source: Internet
Author: User

Php+ajax make no refresh message board, phpajax Refresh message Board


This article is to share with you a PHP with Ajax implementation of the non-refresh message board, first of all to see the last:

The database connection code is as follows:

<?php$conn = @mysql_connect ("localhost", "root", "root") or Die ("MySQL connection error"); mysql_select_db ("Demo", $conn); mysql_ Query ("Set names ' UTF8 '");? >

The index.php file code is as follows:

 
  
 
 
  
 <title>No refresh display replies</title>

No refresh display replies

<?phpinclude ("conn.php"); $sql = "select * from ' bbs_post ' where ' threadid ' = ' 1 ' ORDER by ID ASC"; $query =mysql_query ($s QL); while ($row = Mysql_fetch_array ($query)) {?> "> <?php echo $row [' title '];? > [<?php echo $row [' username '];? ;]
<?php echo $row [' content '];? >
<?php}?>
Replies
Name:
Title:
Content:

The bbspost.php file code is as follows

<?phpinclude ("conn.php"); $title = $_post["title"]; Get title$content = $_post["Content"]; Get content$username = $_post["username"]; Get Username$threadid = $_post["ThreadId"]; Get threadid$sql= "INSERT into Bbs_post (Title,content,username,threadid)".  VALUES (' $title ', ' $content ', ' $username ', ' $threadId ');  mysql_query ($sql); Returns the last id$responseid=mysql_insert_id () with the INSERT instruction; echo $responseId;? >

Bbs.js file contains a large number of Ajax files, the code is as follows

Before you can save Chinese, create an empty bbs.js file and modify its properties to Utf-8.            var xmlHttp;            The global variable var username used to hold the XMLHttpRequest object;             Used to save name VAR title;            Used to save the title var content;            Used to save content var ThreadID; Used to save the subject number//used to create the XMLHttpRequest object function Createxmlhttp () {//according to window. XMLHttpRequest whether the object exists using a different creation method if (window.         XMLHttpRequest) {xmlHttp = new XMLHttpRequest (); Firefox, Opera and other browsers support the way of creation} else {xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");//ie browser support How to create}}//submit replies to Server Funct  Ion Submitpost () {//Get the name, title, content, subject number four part information username = document.getElementById ("username") in the post. Value;  title = document.getElementById ("Post_title"). Value;  Content = document.getElementById ("Post_content"). Value;  ThreadID = document.getElementById ("ThreadID"). Value;                  if (Checkform ()) {createxmlhttp ();  Create XMLHttpRequest Object xmlhttp.onreadystatechange = Submitpostcallback;     Set the callback function Xmlhttp.open ("POST", "bbspost.php", true); Send POST request//Set POST request body type XMLHTTp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Xmlhttp.send ("username=" + encodeURI (username) + "&title=" + encodeURI (title) + "&content=" +       encodeURI (content) + "&threadid=" + ThreadID);    Send the request body with four parameters}}//check if the form is filled out function checkform () {if (username = = "") {alert ("Please fill in name");  return false;    } else if (title = = "") {alert ("Please fill in the title");  return false;    } else if (content = = "") {alert ("Please fill in");  return false; } return true;    Gets the query option for the callback function Submitpostcallback () {if (xmlhttp.readystate = = 4) {alert (xmlhttp.responsetext);  Createnewpost (Xmlhttp.responsetext);  }}//Create a new reply function Createnewpost (PostID) {//empties the information document.getElementById ("Post_title") for each part of the current form. Value = "";  document.getElementById ("Post_content"). Value = "";  document.getElementById ("username"). Value = "";  var postdiv = Creatediv ("Post", "");      Create a reply to the outer div postdiv.id = "post" + PostID; Assign ID value var posttitlediv to new Div = Creatediv ("Post_title", title + "[" + Username + "]"); Create title div var postcontentdiv = creatediv ("Post_content", "
"+ Content +"
"); Create Content Div Postdiv.appendchild (posttitlediv); Append the caption Postdiv.appendchild (POSTCONTENTDIV) to the outer div; Append content document.getElementById ("thread") to the outer Div. appendchild (postdiv); Append the outer div to the theme div}//create a new divfunction Creatediv (className, text) {var newdiv = document.createelement According to className and text) ( "Div"); Newdiv.classname = ClassName; newdiv.innerhtml = text; return newdiv;}

The Bbs.css file is as follows:

/* page basic style */body, TD, input, textarea {  font-family:arial;  font-size:12px;} /* Style of Theme */#thread {  border:1px solid black;  width:300px;  margin-bottom:10px;} /* Tip Info div style */#statusDiv {  border:1px solid #999;  Background: #FFFFCC;  width:100px;  Position:absolute;  top:50%;  left:50%;  margin:-50px 0 0-100px;  width:280px;} /* style of the post */div.post {  border-bottom:1px solid black;  padding:5px;} /* The style of the post title */div.post_title {  border-bottom:1px dotted #0066CC;  Font-weight:bold;} /* style of post content */div.post_content {  font-size:12px;  margin:5px;} /* reply form basic style */table.reply {  border-collapse:collapse;  width:300px;} /* Reply table cell style */table.reply td {  border:1px solid black;  padding:3px;} /* Reply table header style */table.reply td.title {  background: #003366;  Color: #FFFFFF;} /* FORM element style */input, textarea {  border:1px solid black;} /* Text area style */textarea {  width:200px;  height:50px;} /* Predefined format style */pre {  margin:0;}

The above is the whole content of this article, I hope that everyone's study has helped.

http://www.bkjia.com/PHPjc/1065581.html www.bkjia.com true http://www.bkjia.com/PHPjc/1065581.html techarticle php+ajax make no refresh message board, PHPAJAX Refresh Message board This is to share with you a PHP with Ajax implementation of the non-refresh message board, first to show you The last: number ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.