Jquery + ajax + xml + php simple message board-jQuery -.

Source: Internet
Author: User

Demo address http://www.cnjquery.com/demo/gb/guestbook.html

If you don't know how to post on the Forum

Guestbook.html

<! 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>
<Title> brushless jquery php xml Message Board </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Style>
* {Margin: 0; padding: 0}
Ul {list-style: none}
# Msglist {width: 800px; vertical-align: text-top; margin: 20px auto 0px auto ;}

Dt {background: url(wtp-m.png); padding: 5px}
Dl {border: solid 1px #8db2e3}
Dd {padding: 5px}
# Page span {cursor: pointer ;}
# Commentformbox {width: 800px; vertical-align: text-top; margin: 20px auto 0px auto ;}
# Commentformbox TEXTAREA {width: 400px; height: 100px; vertical-align: text-top; margin: 20px auto 0px auto ;}
</Style>
<Script language = "JavaScript" src = "js/jquery-1.2.6.js"> </script>
<Script language = "JavaScript">
<! --
$ (Document). ready (function (){

Function load_msg (page)
{
Certificate ('{state'}.html ("Current status: loading data ....");
Page00000000(this0000.html ();
Page = "undefined "? 1: page;
 
$. Post ("comments-ajax.php", {action: "load", page: page}, function (data ){
// Alert ($ (data). find ("span"). text ());
 
$ ('# Commentlist dl'hangzhou.html (data); // append message data
$ ('# Commentlist dl'). find ("span"). bind ("click", load_msg );
Certificate ('{state'}.html ("Current status: data loaded ");
})
 
}

Load_msg ();
If ($ ('# commentform'). length ){
$ ('# Commentform'). submit (function (){
JQuery. ajax ({
Url: 'comments-ajax. php', // here you want to change to the location of the comments-ajax.php File
Data: $ ('# commentform'). serialize (), // obtain data from the form
Type: 'post', // set the request type to 'post'. The default value is 'get'
BeforeSend: function (){
Condition ('{state'}.html ("Current status: sending data .... ");
},
Error: function (request ){
Response ('{state'}.html (request. responseText );
},
Success: function (data ){
$ ('Textarea '). each (function (){
This. value = '';
});

('{State'{.html ("Current status: data published successfully ");
$ ('# Commentlist dl'). prepend (data); // append message data
$ ('# Commentform: input'). attr ('Disabled', true );
$ ('# Commentformbox'). fadeOut (1000 );
$ ('# Commentload'). hide ();
SetTimeout (function () {// submit a message for 15 seconds before submitting a new message
$ ('# Commentform: input'). removeAttr ('Disabled ');
$ ('# Commentformbox'). fadeIn (15000 );
},5000 );
}
});
Return false;
});
}
})

// -->
</SCRIPT>
<Body>
<Div ID = "state"> Current Status </div>
<Div ID = "msglist">
<Div id = "commentlist">
<Dl>

</Dl>
</Div>
</Div>

<Div id = commentformbox>
<Form method = post action = "" ID = "commentform">
<Textarea name = "Content" ROWS = "" COLS = ""> </TEXTAREA> <br> <input type = "submit" value = "publish information">
</FORM>
</Div>
</Body>

Comments-ajax.php

<? Php
// ================================================ ====
// File name: comments-ajax.php
// Description: simple jquery php XML message book
// Power by cnjquery.com
// ================================================ ====
If ($ _ SERVER ["REQUEST_METHOD"]! = "POST "){
Header ('Allow: Post ');
Header ("HTTP/1.1 405 Method Not Allowed ");
Header ("Content-type: text/plain ");
Exit;
}

Class guestbook extends DOMDocument
{
Var $ pagenum = 10;

Public function _ construct ()
{
Parent: _ construct ();
If (! File_exists ("guestbook. xml "))
{
$ Xmlstr = "<? Xml version = '1. 0'?> <Msglist> </msglist> ";
$ This-> loadXML ($ xmlstr );
$ This-> save ("guestbook. xml ");
}
Else
$ This-> load ("guestbook. xml ");
}
Public function loadMessage ($ page)
{
$ Page = ($ page> 0 )? $ Page: 1;
$ Num = ($ page-1) * $ this-> pagenum;
$ Roots = $ this-> getElementsByTagName ("msglist ");
// Print_r ($ roots );
$ Root = $ roots-> item (0 );
$ Msg = $ root-> getElementsByTagName ("msg ");
$ Total = $ msg-> length;
// Echo $ total;
$ Totalpage = ceil ($ total/$ this-> pagenum );
$ Totalpage = $ totalpage? $ Totalpage: 1;
$ Startid = $ total <$ num? $ Total: $ total-$ num;
$ Listnum = $ startid> $ this-> pagenum? $ Startid-$ this-> pagenum: 0;
// Echo $ startid;
// Echo $ listnum;
For ($ I = $ startid-1; $ I >=$ listnum; $ I --)
{

Echo "<dt> & nbsp; ip :". $ msg-> item ($ I)-> getElementsByTagName ("ip")-> item (0)-> nodeValue. "& nbsp; time :". $ msg-> item ($ I)-> getElementsByTagName ("time")-> item (0)-> nodeValue. "</dt> <dd> ". trim (nl2br ($ msg-> item ($ I)-> getElementsByTagName ("content")-> item (0)-> nodeValue )). "</dd> ";
}
Echo "<dt ID = page> ";
For ($ I = 1; $ I <= $ totalpage; $ I ++)
{
Echo "& nbsp; <span>". $ I. "</span> ";
}
Echo "</dt> ";

}
Public function saveMessage ($ clientIP, $ clientTime, $ postContent)
{
$ Msglists = $ this-> getElementsByTagName ("msglist ");
$ Msglist = $ msglists-> item (0 );
$ Msg = $ this-> createElement ("msg ");
$ Ip = $ this-> createElement ("ip ");
$ Ip-> appendChild ($ this-> createTextNode ($ clientIP ));
$ Msg-> appendChild ($ ip );
$ Time = $ this-> createElement ("time ");
$ Time-> appendChild ($ this-> createTextNode ($ clientTime ));
$ Msg-> appendChild ($ time );
$ Content = $ this-> createElement ("content ");
$ Content-> appendChild ($ this-> createCDATASection (nl2br (SafeHtml ($ _ POST ["Content"]);
$ Msg-> appendChild ($ content );
$ Msglist-> insertBefore ($ msg );
$ This-> save ("guestbook. xml ");
Echo "<dt> & nbsp; ip :". $ clientIP. "& nbsp; time :". $ clientTime. "</dt> <dd> ". $ postContent. "</dd> ";
}
}
$ Guestbook = new guestbook;
If ($ _ POST ["action"] = "load ")
{
$ Guestbook-> loadMessage (intval ($ _ POST ["page"]);
Exit;
}
$ IP = $ _ SERVER ["SERVER_ADDR"];
$ Time = date ("Y-m-d H: I: s ");
$ Content = nl2br (SafeHtml ($ _ POST ["Content"]);
$ Guestbook-> saveMessage ($ IP, $ time, $ content );
Exit;

// Http://hellobmw.com/archives/ajax-comments-with-jquery-for-wordpress.html
Function SafeHtml ($ msg = "", $ clear_script = true)
{
If (empty ($ msg ))
{
Return false;
}
$ Msg = str_replace ('& amp;', '&', $ msg );
$ Msg = str_replace ('& nbsp;', '', $ msg );
If (get_magic_quotes_gpc ())
{
$ Msg = str_replace ("'", "& #39;", $ msg );
$ Msg = str_replace ('"'," & quot; ", $ msg );
}
Else
{
$ Msg = str_replace ("'", "& #39;", $ msg );
$ Msg = str_replace ('"'," & quot; ", $ msg );
}
$ Msg = str_replace ("<", "& lt;", $ msg );
$ Msg = str_replace (">", "& gt;", $ msg );
$ Msg = str_replace ("", "& nbsp;", $ msg );
$ Msg = str_replace ("", "", $ msg );
$ Msg = str_replace ("", "& nbsp;", $ msg );
If ($ clear_script)
{
$ Msg = preg_replace ("/<script (.*?) </Script>/is "," ", $ msg );
$ Msg = preg_replace ("/<frame (. *?)> /Is "," ", $ msg );
$ Msg = preg_replace ("/<iframe (. *?)> /Is "," ", $ msg );
$ Msg = preg_replace ("/</fram (. *?)> /Is "," ", $ msg );
}
Return $ msg;
}

?>

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.