Jquery processes XML instances

Source: Internet
Author: User

<! 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>
</Title>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">

<Style type = "text/CSS">
Body {margin: 0; padding: 0; font-size: 12px;} # messagewindow {Height: 250px; Border: 1px solid; padding: 5px; overflow: auto ;} # wrapper {margin: auto; width: 438px ;}
</Style> <! -- Introduce jquery --> <SCRIPT type = "text/JavaScript">
// <! [CDATA [
$ (Function (){
// Define the timestamp
Timestamp = 0;
// Call the information update function
Updatemsg ();
// Form submission
$ ("# Chatform"). Submit (function (){
$. Post ("backend. php ",{
Message: $ ("# MSG"). Val (),
Name: $ ("# Author"). Val (),
Action: "postmsg ",
Time: Timestamp
}, Function (XML ){
// Clear the content of the information text box
$ ("# MSG"). Val ("");
// Call the function for parsing XML
Addmessages (XML );
});
Return false; // block form submission
});
});
// Information update function, which reads data from the server at a specified time
Function updatemsg (){
$. Post ("backend. php", {time: Timestamp}, function (XML ){
// Remove wait prompt
$ ("# Loading"). Remove ();
// Call the function for parsing XML
Addmessages (XML );
});
// Read every 4 seconds.
SetTimeout ('updatemsg () ', 4000 );
}
// Parse the XML document function and display the data to the page
Function addmessages (XML ){
// Terminate if the status is 2
If ($ ("status", XML). Text () = "2") return;
// Update the timestamp
Timestamp = $ ("time", XML). Text ();
// $. Each cyclic data
$ ("Message", XML). Each (function (){
VaR author = $ ("author", this). Text (); // publisher
VaR content = $ ("text", this). Text (); // content
VaR htmlcode = "<strong>" + author + "</strong>:" + content + "<br/> ";
$ ("# Messagewindow"). prepend (htmlcode); // Add it to the document
});
}
//]>

</SCRIPT>

</Head> <body>

<Div id = "wrapper">

<P id = "messagewindow">

<Strong>
$ Message [user]
</Strong>

: $ Message [MSG]
<Br>

<Strong>
$ Message [user]
</Strong>

: $ Message [MSG]
<Br>

<Strong>
$ Message [user]
</Strong>

: $ Message [MSG]
<Br>

<Strong>
$ Message [user]
</Strong>

: $ Message [MSG]
<Br>
</P> <Form ID = "chatform" Action = "#">
Name:
<Input id = "author" size = "50" type = "text">
<Br>
Content:
<Input id = "MSG" size = "50" type = "text">
<Br>
<Input value = "send" type = "Submit">
<Br>
</Form>

</Div>

</Body>

</Html>

<? PHP
// Configuration information:
// 1. database connection details
// 2. Number of messages to be stored
// 3. The number of messages displayed when the user enters the chat room
$ Dbhost = "localhost ";
$ Dbuser = "root ";
$ Dbpass = "root ";
$ Dbname = "chat ";
$ Store_num = 10;
$ Display_num = 10;

// Error Report
Error_reporting (e_all );

// Header information
Header ("Content-Type: text/XML ");
Header ("cache-control: No-Cache ");

// Connect to MySQL
$ Dbconn = mysql_connect ($ dbhost, $ dbuser, $ dbpass );
Mysql_select_db ($ dbname, $ dbconn );

// For easy operation of request data, we set a variable for each parameter in the request. Each variable will take the parameter value in the request as its own value.
// The foreach statement traverses all post data, creates a variable for each parameter, and assigns a value to it
Foreach ($ _ post as $ key => $ value ){
$ Key = mysql_real_escape_string ($ value, $ dbconn );
}

// Screen out any error message to determine whether the action is postmsg
If (@ $ action = "postmsg "){
// Insert data
Mysql_query ("insert into messages ('user', 'msg ', 'time ')
Values ('$ name',' $ message', ". Time ().") ", $ dbconn );
// Delete data (because we store 10 data records by default)
Mysql_query ("delete from messages where ID <= ".
(Mysql_insert_id ($ dbconn)-$ store_num), $ dbconn );
}

// Query data
$ Messages = mysql_query ("Select User, MSG
From messages
Where time> $ time
Order by ID ASC
Limit $ display_num ", $ dbconn );
// Whether a new record exists
If (mysql_num_rows ($ messages) = 0) $ status_code = 2;
Else $ status_code = 1;

// Return the XML Data Structure
Echo "<? XML version =/"1.0/"?> /N ";
Echo "<response>/N ";
Echo "/T <status> $ status_code </status>/N ";
Echo "/T <time>". Time (). "</time>/N ";
If ($ status_code = 1) {// if there is a record
While ($ message = mysql_fetch_array ($ messages )){
$ Message ['msg '] = htmlspecialchars (stripslashes ($ message ['msg']);
Echo "/T <message>/N ";
Echo "/T <author> $ message [user] </author>/N ";
Echo "/T <text> $ message [MSG] </text>/N ";
Echo "/T </message>/N ";
}
}
Echo "</response> ";

?>


Generated XML file !!

<? XML version = "1.0" encoding = "UTF-8"?>
<Response>
<Status> 1 </status>
<Time> 1170323512 </time>
<Message>
<Author> Zhang San </author>
<Text> sofa! </Text>
</Message>
<Message>
<Author> Li Si </author>
<Text> bench! </Text>
</Message>
</Response>

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.