如何使用Ajax连接数据库检查输入验证(jsp)

问题描述 投票:1回答:2

代码是检查用户的大学输入是否已存在于数据库中。如果是,则提交输入并转到下一页;如果没有,则向用户发送警报消息并保持在同一页面上,即choose_university.jsp。 checkUniversity.jsp用于连接数据库并进行检查。但是代码没有这样做。我已经花了几个小时,仍然无法弄明白。谁能告诉我它有什么问题,并告诉我如何解决它?它将在明天到期。请帮我。

choose_university.jsp如下:

<%@page import="java.util.*"%>

 <html>
 <head><title>Provide degrees - choose university</title> 
 <script type="text/javascript">

 function validate() {

     var xmlHttp;
     xmlHttp = new XMLHttpRequest();
     if (xmlHttp == null) {
     alert("Your browser does not support AJAX!");
     return;
      }
     var u = document.getElementById("university").value;
     var url = "checkUniversity.jsp";
     url = url + "?university=" + u;
     xmlHttp.onreadystatechange = function() {
     if (xmlhttp.readyState == 4 ) {
          document.getElementById("university").innerHTML = xmlhttp.responseText;
        }
     }
     alert("yea we got 55555");
     xmlHttp.open("GET", url, true);
     xmlHttp.send(null);
  }



  function GetXmlHttpObject() {
      var xmlHttp = null;
      try {
          // Firefox, Opera 8.0+, Safari

           xmlHttp = new XMLHttpRequest();
      } catch (e) {
            // Internet Explorer
     try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
       }
     }
     return xmlHtp;
  }
   </script>
   </head>
    <body>
   <br> If you can't find your university, please provide it in the following and hit submit <br>
   <form method="post" action="Provide_degrees_Choose_discipline.jsp" onsubmit = "return validate()">
    <p>To manually add your university </p> <br>
    <p> name of university: <input type = "text" id="university"  name = "university" />        </p><br>
    <input type="submit" name = "submit" value="submit"  />
    </form>
     </body>
     </html>



       /*  checkUniversity.jsp  */
      <% response.setContentType("text/xml") ; %>

     <%@ page import="javax.sql.*"%>
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
      <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
      <%@ page import="model.ApplicationModel" %>
    <html>
     <head><title>check university</title> 
    </head>
     <body>
     <%
          System.out.println("heyheyhey");

           String u = request.getParameter("university") ;
    Class.forName("org.postgresql.Driver");
        // Open a connection to the database using DriverManager
        conn = DriverManager.getConnection(
                "jdbc:postgresql://localhost:5432/access?" +
                "user=postgres&password=neshorange");
        // Create the statement
        Statement statement = conn.createStatement();
            // Use the created statement to SELECT
            // the student attributes FROM the Student table.

        rs = statement.executeQuery("SELECT count(*) as c FROM universities WHERE university=\'"+ u +"\';");
      if (rs.next()){
       if ( rs.getInt("c") > 0) {
           response.write("false");
       } else {
       response.write("true");
       }
      }
       response.write("true");
        %>
    </body>
    </html>
javascript ajax javascript-events onsubmit
2个回答
0
投票

尝试摆脱onSubmit中的“返回”,然后重试。还要安装firebug或其他检查员(如果你还没有),这样你就可以看到javascript并请求错误。

这些天也没有必要经历这样的所有ajax。看看像jQuery或Mootools这样的javascript库。他们可以将你的js代码转换成几行。


0
投票

试试这个

function validate()
{
    var u = document.getElementById("university").value;
    $.post('checkUniversity.jsp?university=' + u, function(data) {
      if(data==true) return true;
      else
          {
              alert("user doesnot exists ")
              return false;
          }
});
}

如果用户来自checkUniversity.jsp,则返回true

© www.soinside.com 2019 - 2024. All rights reserved.