Ajax used to determine whether a user exists

Source: Internet
Author: User

The Ajax (JavaScript) function of the registration page is used to determine whether a user exists, so that the page Passes parameters from the background without refreshing the page. <SCRIPT type = "text/JavaScript">
VaR http_request = false;
VaR is_open = false;

Function callserver (){

VaR user = Document. getelementbyid ("username"). value;
If (user = NULL) | (user = "") return false;

Http_request = false;

// Start initializing the XMLHTTPRequest object
If (window. XMLHttpRequest) {// Mozilla Browser
Http_request = new XMLHttpRequest ();

If (http_request.overridemimetype) {// sets the mime category
Http_request.overridemimetype ("text/XML ");
}
} Else if (window. activexobject) {// IE browser
Try {
Http_request = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Http_request = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}

If (! Http_request) {// exception. An error occurred while creating the object instance.
Window. Alert ("the XMLHTTPRequest object instance cannot be created .");
Return false;
}

Http_request.onreadystatechange = processrequest;

// Determine the request sending method and URL and whether to execute the following code synchronously
VaR url = "http: // localhost: 8086/bayestore/ajax. jsp? Username = "+ user;
Http_request.open ("get", URL, false );
Http_request.send (null );


}

// Function for processing the returned information
Function processrequest (){
If (http_request.readystate = 4) {// judge the object status
If (http_request.status = 200) {// The information has been returned successfully. Start to process the information.
Document. getelementbyid ("checkuser"). value = http_request.responsetext;
Alert (http_request.responsetext );
} Else {// The page is abnormal.
Alert ("the page you requested has an exception. ");
}
}
}
</SCRIPT>

Register the JSP form tag on the page

<Form action = "register. Do" name = "userregister" method = "Post">

<Table width = 750 border = 0 cellpadding = 3 cellspacing = 0 bgcolor = "# f3f9ff">

<Tbody>
<Tr>
<TD colspan = 5 Height = 5> </TD> </tr>
<Tr>
<TD width = 60 Height = 25>
<Div align = right> * </div> </TD>
<TD width = 92 Height = 25> User name: </TD>
<TD width = 172 Height = 25> <input type = "text" name = "username"> </TD>
<TD width = 101 Height = 25> <Div align = center> <input type = "image" src = "image/buy_yanzheng.jpg" name = "checkuser" id = "checkuser" onclick = "callserver (); return false; "> </div> </TD>
<TD width = 297 Height = 25> (verify whether your user name is occupied) </TD> </tr>
<Tr>
<TD Height = 25>
<Div align = right> * </div> </TD>
<TD Height = 25> password: </TD>
<TD colspan = 3 Height = 25> <input type = "password" name = "password"> & nbsp; (set a password of more than 6 characters) </TD> </tr>
<Tr>
<TD Height = 25>
<Div align = right> * </div> </TD>
<TD Height = 25> Confirm Password: </TD>
<TD colspan = 3 Height = 25> <input type = "password" name = "repassword"> & nbsp; (enter the password again) </TD> </tr>
<Tr>
<TD Height = 25>
<Div align = right> * </div> </TD>
<TD Height = 25> User name: </TD>
<TD colspan = 3 Height = 25> <input type = "text" name = "zsname"> & nbsp; (Please leave your real name so that we can contact you, delivery for you !) </TD> </tr>
<Tr>
<TD Height = 25>
<Div align = right> * </div> </TD>
<TD Height = 25> sex: </TD>
<TD colspan = 3 Height = 25> <select class = buy_box name = usersex>
<Option selected> </option> <option value = male> male </option>
<Option value = female> female </option> </SELECT> </TD> </tr>
<Tr style = "line-Height: 30px">
<TD Height = 25>
<Div align = right> * </div> </TD>
<TD Height = 25> contact number: </TD>
<TD colspan = 3 Height = 25> <input type = "text" name = "telephone"> (enter the area code and phone number) </TD>
</Tr>
<Tr>
<TD Height = 25>
<Div align = right> * </div> </TD>
<TD Height = 25> Email: </TD>
<TD colspan = 3 Height = 25> <input type = "text" name = "email"> (enter an email address) </TD> </tr>
<Tr>
<TD Height = 25>
<Div align = right> * </div> </TD>
<TD Height = 25> Mailing address: </TD>
<TD colspan = 3 Height = 25> <textarea name = "Address" rows = 3 Cols = 60> </textarea> </BR> (enter the correct mailing address, in order to accurately and timely deliver the goods you ordered) </TD> </tr>
<Tr>
<TD Height = 25>
<Div align = right> * </div> </TD>
<TD Height = 25> zip code: </TD>
<TD colspan = 3 Height = 25> <input type = "text" name = "zipcode" size = "2" maxlength = "6"> (Please enter a 6-digit ZIP code) </TD> </tr>
<Tr>
<TD Height = 25> <Div align = right> * </div> </TD>
<TD Height = 25> credit card number: </TD>
<TD colspan = 3 Height = 25> <input type = "text" name = "idcard">
(Enter a 16-digit credit card number) </TD>
</Tr>
<Tr>
<TD Height = 25> & nbsp; </TD>
<TD Height = 25> & nbsp; </TD>
<TD colspan = 3 Height = 25> & nbsp; </TD> </tr>
<Tr>
<TD colspan = 2> </TD> </tr>
<Tr>
<TD Height = 46> & nbsp; </TD>
<TD colspan = 4> <br> </TD>
</Tr>
<Tr>
<TD Height = 46>
</TD>
<TD colspan = 4>
<HR Align = left width = 600 size = 1>

<Div align = center>
<Input type = "Submit" name = "Submit" value = "register" onclick = "checkuserreg ('add'); Return false;"/>
<Input type = "reset" name = "reset" value = "Refill">
<Input name = "action" type = "hidden" value = ""/>
</Div> </TD> </tr>
</Table> </form>

Ajax is sent to the processing and response page:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<% @ Page import = "com. Bay. Bean. *" %>
<%
String username = request. getparameter ("username ");
User user = new user ();
User. setusercode (username );
If (user. ischeckreg ())
{Out. Write ("congratulations, this user can register and use ");
} Else {out. Write ("sorry, this user name already exists, re-enter ");}
%>

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.