|
// Create an empty bbs. js file and modify its attribute to UTF-8 to save Chinese characters.
Var xmlHttp; // global variable used to save the XMLHttpRequest object
Var username; // used to save the name
Var title; // used to save the title
Var content; // used to save the content
Var threadid; // used to save the topic ID.
// Create an XMLHttpRequest object
Function createXmlHttp (){
// Use different creation methods based on whether the window. XMLHttpRequest object exists
If (window. XMLHttpRequest ){
XmlHttp = new XMLHttpRequest (); // creation method supported by FireFox, Opera, and other browsers
} Else {
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP"); // creation method supported by ie
}
}
// Submit a reply to the server
Function submitPost (){
// Obtain the name, title, content, and topic number of a post.
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 (); // Create an XMLHttpRequest object
XmlHttp. onreadystatechange = submitPostCallBack; // sets the callback function.
XmlHttp. open ("POST", "bbs_post.php", true); // send a POST request
// Set the 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 a request body containing four parameters
}
}
// Check whether the content of the form has been filled in
Function checkForm (){
If (username = ""){
Alert ("Please enter your name ");
Return false;
} Else if (title = ""){
Alert ("Enter the title ");
Return false;
} Else if (content = ""){
Alert ("Enter the content ");
Return false;
}
Return true;
}
// Obtain the callback function of the query option
Function submitPostCallBack (){
If (xmlHttp. readyState = 4 ){
Alert (xmlHttp. responseText );
CreateNewPost (xmlHttp. responseText );
}
}
// Create a new reply
Function createNewPost (postId ){
// Clear the information of each part of the current form
Document. getElementById ("post_title"). value = "";
Document. getElementById ("post_content"). value = "";
Document. getElementById ("username"). value = "";
Var postDiv = createDiv ("post", ""); // create the outer div of the Reply
PostDiv. id = "post" + postId; // assign an id value to the new div
Var postTitleDiv = createDiv ("post_title", title + "[" + username + "]"); // create the title div
Var postContentDiv = createDiv ("post_content", "<pre>" + content + "</pre>"); // create a content div
PostDiv. appendChild (postTitleDiv); // Append the title to the outer div.
PostDiv. appendChild (postContentDiv); // append content to the outer div
Document. getElementById ("thread"). appendChild (postDiv); // Append the outer div to the topic div.
}
// Create a new div based on className and text
Function createDiv (className, text ){
Var newDiv = document. createElement ("div ");
NewDiv. className = className;
NewDiv. innerHTML = text;
Return newDiv;
} |