在类别名称唯一之前,避免使用下一个URL

时间:2013-09-02 10:31:01

标签: java javascript ajax json jsp

这是Jsp代码,我将通过使用javascript和ajax来验证我的类别名称,当我按下提交按钮时,它会要求显示的类别名称很好但是同样的我验证类别通过使用ajax调用名称唯一它将检查唯一的蓝色和第一次提交按钮按下当我按第二次它将转到主URL

<form name="frm" action="createnewcatgoryBean.jsp" 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" onclick="return validatenewcat()" style="width: 60px;">
    </td>
    </tr>
    </table>
    </form>

这是我的ajax调用脚本和javascript

  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;
                  if(temp!="")
                     {
                         document.getElementById("validate").innerHTML="!PLz Enter The Unique Category Name";
                         document.getElementById("cat").focus();
                        return false; 
                     }

                  }
          }

        }
    }


    }

这是我的java代码

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"));
                JSONObject json = new JSONObject();
                json.accumulateAll(map);
            }
            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);
       }
}

这是我的servlet代码

System.out.println("inside success");
                JSONObject json = new JSONObject();
                json.accumulateAll(result);
                response.setContentType("application/json");
                response.getWriter().write(json.toString());

当我按第二次提交时,它会转到另一个网址,请帮助我谢谢你

2 个答案:

答案 0 :(得分:1)

我希望这是你面临的问题

<form name="frm" action="createnewcatgoryBean.jsp" method="post" onsubmit="return validatenewcat()">
    <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>

在表单标记中使用提交,而不是单击提交类型

答案 1 :(得分:1)

var cat_array = new array();
    var i=0;
function validatenewcat()
    {
        var is_dup_cat=false;
        var category = document.getElementById("cat").value;
      if(cat_array.length>=1){
       for(var j=0;j<cat_array.length;j++){
       if(cat_array[j]==category ){
       is_dup_cat=true;
         }
         }
       }


        if(category=="" || is_dup_cat)
        {
            setTimeout(document.getElementById("validate").innerHTML="!PLz Enter The Category Name", 2000);
            return false;
        }   
        else
        {
        cat_array[i++]=category ;
        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;
                  if(temp!="")
                     {
                         document.getElementById("validate").innerHTML="!PLz Enter The Unique Category Name";
                         document.getElementById("cat").focus();
                        return false; 
                     }

                  }
          }

        }
    }


    }