我用Ajax验证文本框输入的值在数据库中是否已存在,jsp中JS代码如下:
function send()
{
var typeName=encodeURI(document.getElementById("typeName").value);
createXMLHttpRequest();
xmlHttp.onreadystatechange=callback;
xmlHttp.open("post","<%=request.getContextPath() %>/newsType.do?method=checkNewsType&typeName="+typeName ,true);
xmlHttp.send(null);
}
function callback()
{
if(xmlHttp.readyState==4&&xmlHttp.Status==200)
{
alert(xmlHttp.responseText);
}
}
function check(){
var typeName=document.getElementById("typeName");
if(typeName.value==""||typeName.value.length==0){
alert('请输入您要添加的新闻类型!!');
return false;
}
else{
send();
if(xmlHttp.responseText.length>0){
alert(xmlHttp.responseText);
return false;
}
else{
return true;
}
}}
public ActionForward checkNewsType(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("text/xml");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Charset", "utf-8");
response.addHeader("Charset", "utf-8");
NewsTypeForm newsTypeForm = (NewsTypeForm) form;// TODO Auto-generated method stub
String page="";
PrintWriter out=response.getWriter();
String typeName=new String(request.getParameter("typeName").getBytes("iso-8859-1"),"utf-8");
DataBaseConn db=new DataBaseConn();
ResultSet rs=db.executeQuery("select typeName from newsType where typeName='"+typeName+"'");
if(rs.next()){
out.println("此新闻类型已经存在!");
}
rs.close();
db.closeDB();
return null;
}
这样运行时,当我添加数据库已存在的记录时,会弹出一个对话框提示数据库已存在,但是还能添加成功!!!请问高手,应该如何判断才对啊??先谢谢了!!分不够了,下次有了,再给。
function send()
{
var typeName=encodeURI(document.getElementById("typeName").value);
createXMLHttpRequest();
xmlHttp.onreadystatechange=callback;
xmlHttp.open("post","<%=request.getContextPath() %>/newsType.do?method=checkNewsType&typeName="+typeName ,true);
xmlHttp.send(null);
}
function callback()
{
if(xmlHttp.readyState==4&&xmlHttp.Status==200)
{
alert(xmlHttp.responseText);
}
}
function check(){
var typeName=document.getElementById("typeName");
if(typeName.value==""||typeName.value.length==0){
alert('请输入您要添加的新闻类型!!');
return false;
}
else{
send();
if(xmlHttp.responseText.length>0){
alert(xmlHttp.responseText);
return false;
}
else{
return true;
}
}}
public ActionForward checkNewsType(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("text/xml");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Charset", "utf-8");
response.addHeader("Charset", "utf-8");
NewsTypeForm newsTypeForm = (NewsTypeForm) form;// TODO Auto-generated method stub
String page="";
PrintWriter out=response.getWriter();
String typeName=new String(request.getParameter("typeName").getBytes("iso-8859-1"),"utf-8");
DataBaseConn db=new DataBaseConn();
ResultSet rs=db.executeQuery("select typeName from newsType where typeName='"+typeName+"'");
if(rs.next()){
out.println("此新闻类型已经存在!");
}
rs.close();
db.closeDB();
return null;
}
这样运行时,当我添加数据库已存在的记录时,会弹出一个对话框提示数据库已存在,但是还能添加成功!!!请问高手,应该如何判断才对啊??先谢谢了!!分不够了,下次有了,再给。
应该是这样的:onsubmit=“return check();”
{
alert(xmlHttp.responseText); }
这个的意思貌似是执行完请求返回再判断....就是说你执行完数据库操作了再提示你有重复数据....