How to write AJAX to validate database content and display values on the page
This article introduces the knowledge of "how to write AJAX to verify database content and display values on the page". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Function realization:
Fill in the contents of the text box in the jsp page, leave the text box with the cursor, and get the other data corresponding to the change in the database in the corresponding place on this page.
Servlet:
Request.setCharacterEncoding ("utf-8"); PrintWriter out = response.getWriter (); / / call the servlet layer to the database to find out whether there is the same user name and return to other records on the page String client_id = request.getParameter ("client_id"); ClientServices clientServices = new ClientServices (); try {Thread.sleep (2000);} catch (InterruptedException e) {e.printStackTrace ();} Client client = clientServices.findClientById (client_id) If (client! = null) {out.print (URLEncoder.encode (client.getClient_name (), "utf-8"));} else {out.print ("false");} out.flush (); out.close ()
Jquery:
$(document) .ready (function () {$("# client_id") .blur (function () {$.ajax ({type: 'POST',url:' servlet/validServlet?client_id=' + $(this). Val (), data: 'client_id=' + $("# client_id"). Val (), success: function (msg) {if (msg = =' false') {alert ("no one") } else {/ / utf-8 decoding to solve Chinese garbled $("# clientInfo") .html (decodeURI (msg)); $("# clientInfo"). Attr ("value", decodeURI (msg));}); "how to write AJAX to verify database content and display values on the page" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!