A simple ASP + Ajax chat room (1)

Source: Internet
Author: User

 

Use ASP + ajax to create a simple chat room. Send messages first today.

Front-end code:

 

<! 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"/>
<Title> chat room with no questions </title>
<SCRIPT type = "text/JavaScript" src = "chat. js"> </SCRIPT>
<Style type = "text/CSS">
<! --
. Chat {border: #666666 1px solid; display: block; margin-left: 5px; margin-Right: 5px; Height: 680px; padding: 10px ;}
-->
</Style>
</Head>

<Body>
<H3> none Chat Room <Div id = "chat" class = "chat">
<! -- The Chat information is displayed here -->
</Div>
<Form ID = "frmsend" name = "frmsend" onsubmit = "sendmess (); Return false;">
<Input name = "username" type = "text" id = "username" value = "no question" size = "10"/>
<Input name = "mess" type = "text" id = "mauto" size = "100"/>
<Input type = "Submit" value = "send" id = "submitsend" name = "submitsend"/>
</Form>
</Body>
</Html>

 

JS Code chat. JS:

 

// Create an XMLHTTPRequest object by encapsulating the getajax () method
Function getajax ()
{
VaR Ajax = false;
Try {
Ajax = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Ajax = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){
Ajax = false;
}
}
If (! Ajax & typeof XMLHttpRequest! = 'Undefined '){
Ajax = new XMLHttpRequest ();
}
Return Ajax;
}

VaR getmessreq = getajax (); // gets the XMLHTTPRequest object of the message.
VaR sendmessreq = getajax (); // XMLHTTPRequest object for sending messages

// Message sending Method
Function sendmess ()
{
// If the message is empty, a prompt is returned.
If (document. getelementbyid ("mess"). value = ""){
Alert ("You have not entered a message! ");
Return;
}

// Determine the status of the last message sent, 4: Sent, 0: not sent
If (sendmessreq. readystate = 4 | sendmessreq. readystate = 0 ){
// The server address for sending the message
VaR sendurl = "send. asp? Username = "+ escape (document. getelementbyid (" username "). Value) +" & mess = "+ escape (document. getelementbyid (" mess "). value );
Sendmessreq. Open ("Post", sendurl, true); // create a request connection
Sendmessreq. onreadystatechange = function () {// method called after the sending status changes
// Some code will be written tomorrow
}

Sendmessreq. Send (null); // send the request
Document. getelementbyid ("mess"). value = ""; // set the message box to null.
}
}

 

Send. asp:

 

<% @ Language = "VBScript" codePage = "65001" %>
<! -- # Include file = "conn. asp" -->
<%
Dim username, mess
Username = trim (request. querystring ("username "))
Mess = trim (request. querystring ("mess "))
Conn.exe cute ("insert into message (messtext, username) values ('" & mess & "', '" & username &"')")
Conn. Close () set conn = nothing %>

 

Data Table: Message

Note: Set a default value for the date field: getdate ()

Please wait here. I will write the message display part tomorrow. It's time to go to bed.

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.