这是我的javascript代码当我第二次按下提交按钮进入另一个网址时发生了什么,但在第一次它给出结果正常可以任何人帮助我PLZ
function validatenewcat(){
var category = document.getElementById("cat").value;
if(category==""){
setTimeout(document.getElementById("validate").innerHTML="!PLz Enter The Category Name", 2000);
return false;
}
else{
var url="catnamecheck.do?id="+category;
xmlhttp.open("post", url,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
var temp = xmlhttp.responseText;
obj = JSON.parse(temp);
if(obj.catgoryname==category) {
alert(obj.catgoryname);
alert(category);
document.getElementById("validate").innerHTML="!PLz Enter The Unique Category Name";
return false;
}
if(obj.catgoryname!=category){
alert("inside true"+obj.catgoryname);
alert("inside true"+category);
return true;
}
}
}
}
}
}
这是我的java代码,我从ajax
调用此代码public Map<String, String> catuniqecheck(String id) {
Connection c = null;
PreparedStatement ps1 = null;
ResultSet rs1 = null;
String sql=null;
try{
c = JDBCHelper.getConnection();
if(c!=null)
{
Map<String, String> map = new HashMap<String, String>();
sql="select * from catgory where catgoryname=?";
ps1=c.prepareStatement(sql);
ps1.setString(1, id);
ps1.execute();
rs1=ps1.getResultSet();
if(rs1.next())
{
System.out.println("insdide of the catuniqecheck");
map.put("catgoryname",rs1.getString("catgoryname"));
}
return map;
}
else
{
System.out.println("DB connection Established");
return null ;
}
}
catch (Exception e) {
// TODO: handle exception
return null ;
}
finally{
JDBCHelper.close(rs1);
JDBCHelper.close(ps1);
JDBCHelper.close(c);
}
}
这是我的sevlet代码
if (uri.contains("/catnamecheck.do")) {
Project p1=new Project();
String id=request.getParameter("id");
System.out.println("inside uniq check id"+id);
Map<String, String> result =p1.catuniqecheck(id);
if(result!=null)
{
System.out.println("inside success");
JSONObject json = new JSONObject();
json.accumulateAll(result);
System.out.println("inside json"+json.toString());
response.setContentType("application/json");
response.getWriter().write(json.toString());
}
if(result==null)
{
System.out.println("inside filure");
}
}
这是我的jsp代码
<form name="frm" action="createnewcatgoryBean.jsp" onsubmit="return validatenewcat()" method="post">
<table style="padding: 5px 0px 0px 8px;">
<tr>
<th colspan="2">
<div style="width: width:271px; color:red;" id="validate"></div>
</th>
</tr>
<tr>
<th>Category Name<span>:</span></th><td><input id="cat" onblur="return validatenewcat()" type="text" name="category">
</td>
</tr>
<tr>
<th>Quotations form<span>:</span></th><td><input type="checkbox" name="quotations"></td>
</tr>
<tr>
<th>Agreement form<span>:</span></th><td><input type="checkbox" name="agreement"></td>
</tr>
<tr>
<th>Payment form<span>:</span></th><td><input type="checkbox" name="payment"></td>
</tr>
<tr>
<th>ETI<span>:</span></th><td><input type="checkbox" name="eti"></td>
</tr>
<tr>
<td colspan="2" style="float:right; padding-top:15px">
<input type="submit" value="Submit" style="width: 60px;">
</td>
</tr>
</table>
</form>
答案 0 :(得分:0)
您已设置表单action="createnewcatgoryBean.jsp"
。如果验证成功,它将提交到此页面。为避免提交,请从您的javascript返回false。
else {
var url = "catnamecheck.do?id=" + category;
xmlhttp.open("post", url, true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var temp = xmlhttp.responseText;
obj = JSON.parse(temp);
if (obj.catgoryname == category) {
alert(obj.catgoryname);
alert(category);
document.getElementById("validate").innerHTML = "!PLz Enter The Unique Category Name";
return false;
}
if (obj.catgoryname != category) {
alert("inside true" + obj.catgoryname);
alert("inside true" + category);
return true;
}
}
}
}
return false; //return false to avoid form submission
}