Simple Ajax application to check whether the user name exists

Source: Internet
Author: User
Client page index.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> Ajax test </title>
<Script language = "JavaScript" type = "text/JavaScript">
// Generate an HTTP request object for the client to send an asynchronous HTTP request to the service/end
Function gethttpobject ()
{
VaR HTTP;
VaR browser = navigator. appname;

If (Browser = "Microsoft Internet Explorer ")
{
// If you use IE, the ActiveX object of XMLHTTP is returned.
HTTP = new activexobject ("msxml2.xmlhttp. 3.0 ");
}
Else
{
// Otherwise, an XMLHTTPRequest object is returned.
HTTP = new XMLHttpRequest ();
}

Return HTTP;
}
// Obtain the global HTTP request object
VaR HTTP = gethttpobject ();

// Process Request status changes
Function gethello ()
{
// 4 indicates that the request has been completed
If (HTTP. readystate = 4)
{

// Obtain the response text of the Service segment

VaR hellostr = http. responsetext;
Alert (hellostr );
If (hellostr. charat (0 )! = "0 ")
{Alert ("the user name already exists! ");}
Else
{Alert ("the user name does not exist. It can be used! ");}
}
}
Function helloworld ()
{
VaR url = "check. aspx? Id = "+ document. getelementbyid (" text1 "). value;

// Specify the server address
HTTP. Open ("get", URL, true );
// Processing function when the Request status changes
HTTP. onreadystatechange = gethello;
// Send the request
HTTP. Send (null );
}

</SCRIPT>

</Head>
<Body>
<Input id = "text1" style = "width: pixel PX" type = "text"/>
<Input id = "button1" type = "button" value = "button" onclick = "javascript: helloworld ();"/>

</Body>
</Html>

Server Function page check. aspx

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;

Public partial class check: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String STR = "select count (*) from table where name =" + request. querystring ["ID"]. tostring () + "";
Sqlconnection conn = new sqlconnection ("Data Source =.; initial catalog = database; Integrated Security = true ");
Conn. open ();
Sqlcommand cmd = new sqlcommand (STR, Conn );
Int I = (INT) cmd. executescalar ();
Conn. Close ();
Response. Write (I );
}
}

Transferred from: the sky released by relaxmyself

Related Article

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.