使用AJAX将隐藏变量解析为另一个jsp文件

时间:2013-10-17 05:32:38

标签: javascript ajax jsp

我想用AJAX显示一个隐藏值,但是当我按下提交按钮并将我重定向到另一个页面时,它不会停留在同一页面上。我的脚本标签如下所示。脚本标签上方的任何内容都可以。我想知道是否有人可以帮我解决这个问题。

<script>
    function loadXMLDoc() {
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
            }
        }
        var hiddenUserID = encodeURIComponent(document.getElementBuId("hiddenID").value);
        xmlhttp.open("GET", "displayUserScore.jsp?hUserID="+hiddenUserID,true);
        xmlhttp.send(null);
    }
</script>
</head>
<body>
    <%
        String user1 = request.getParameter("compareUser1");
        String user2 = request.getParameter("compareUser2");
        out.println(user1 + "&nbsp" + user2);
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager
                .getConnection("jdbc:mysql://localhost/survey?user=root&password=dso123");
        String retrieveUserData = "SELECT u.userID, u.fullName, sum(uma.isCorrect) score, u.endTime FROM users u, usermcqanswer uma WHERE u.userID = uma.userID AND u.fullName = ?";
        PreparedStatement pstmt = conn.prepareStatement(retrieveUserData);
        pstmt.setString(1, user1);
        ResultSet rs = pstmt.executeQuery();
    %>
    <table border=1>
        <tr>
            <td>User ID</td>
            <td>Full name</td>
            <td>Score</td>
            <td>Other information</td>
        </tr>
        <%
            while (rs.next()) {
                out.println("<tr>");
                out.println("<td>" + rs.getString("userID") + "</td>");
                out.println("<td>" + rs.getString("fullName") + "</td>");
                out.println("<td>" + rs.getString("score") + "</td>");
                out.println("<td>");
        %>
        <form action="displayUserScore.jsp" method="get">
            <input type="hidden" value=<%=rs.getString("userID")%> id="hiddenID">
            <input type="submit" onclick="loadXMLDoc()" value="View Scenario Score">
        </form>
        <%
            out.println("</td>");
            }
        %>
    </table>
    <div id="myDiv"></div>
</body>

1 个答案:

答案 0 :(得分:0)

添加引号:

value='<%=rs.getString("userID")%>'

并将type="submit"更改为type="button"

请考虑使用JSTL