Home > Article > Web Front-end > AJAX validates database content and displays the value on the page
The cursor leaves the text box and obtains other data corresponding to the changed value in the database at the corresponding place on this page. The corresponding implementation code is as follows. Interested friends can take a look at
Function implementation:
Fill in the text box content in the jsp page, leave the cursor from the text box, and obtain other data corresponding to the changed value in the database at the corresponding place on this page.
servlet:
request.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); // 调用servlet层去数据库查找是否有相同用户名 并返回到页面中的其他记录 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("没有此人"); } else { //utf-8解码解决中文乱码 $("#clientInfo").html(decodeURI(msg)); $("#clientInfo").attr("value", decodeURI(msg)); } } }); }); });
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
AJAX submission form data instance analysis
Method of ajax reading properties resource file data
A brief discussion on Ajax and its advantages and disadvantages
The above is the detailed content of AJAX validates database content and displays the value on the page. For more information, please follow other related articles on the PHP Chinese website!