Home  >  Article  >  Web Front-end  >  AJAX validates database content and displays the value on the page

AJAX validates database content and displays the value on the page

亚连
亚连Original
2018-05-25 15:36:491590browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn