Ajax verifies that this ID has a corresponding name

Source: Internet
Author: User

 

Enter an ID in the table, and then automatically search for the corresponding name in the database based on the ID

This is the Javascript part, which is verified by Ajax.

$ (Document ). ready (function () {$ ("# client_id "). blur (function () {$. ajax ({type: 'post', URL: 'servlet/valclientservlet ', // This is the background servlet method data: 'client _ id = '+ $ ("# client_id "). val (), // client_id is the ID attribute beforesend: function () {$ ("# progress" ).html ("AAA") ;}, complete: function (XMLHttpRequest, textstatus) {// This is a short sleep before execution. The method is implemented in servlet $ ("# progress" ).html ("") ;}, success: function (MSG) {If (MSG = 'false') {// if it is false, the alert pop-up box prompts No, and make the DIV with the displayed name empty alert ("No such person"); $ ("# client_name" ).html ("");} else {// judge as true, display the name value in the Div. decodeuri () is to prevent garbled $ ("# client_name" ).html (decodeuri (MSG ));}}});});});

 

This is servlet content

Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html"); printwriter out = response. getwriter (); Request. setcharacterencoding ("UTF-8"); response. setcharacterencoding ("UTF-8"); string client_id = request. getparameter ("client_id"); try {thread. sleep (1000); // This row is the sleep before execution, and the number in the brackets is in milliseconds} catch (interruptedexc Eption e) {// todo auto-generated Catch Block E. printstacktrace ();} // call services to go to the database and check whether the same user name backdao = new backdao (); e_client eclient = Dao. findbyid (client_id); // The findbyid () method is a method that contains SQL query statements and queries the database // Boolean isselect = Dao. selectclient (client_id); If (eclient! = NULL) {out. print (urlencoder. encode (eclient. getclient_name (), "UTF-8"); // decodeuri () method that prevents garbled characters in Ajax. You need to add} else {out. print (false);} Out. flush (); out. close ();}

 

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.