1

该代码用于检查来自用户的大学输入是否已存在于数据库中。如果是,则提交输入并转到下一页;如果没有,则向用户发送一条警告消息并停留在同一页面上,即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>
4

2 回答 2

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

于 2011-12-02T05:28:13.803 回答
0

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

这些天也没有必要经历所有像这样的ajax东西。查看 jQuery 或 Mootools 等 JavaScript 库。他们可以把你的 js 代码变成几行代码。

于 2011-12-02T05:13:53.820 回答