2016-12-26 27 views
0

我正在寫一個在線預訂系統應用程序。用戶可以通過複選框選擇一項活動。然後,他們可以提交他們的預訂。我的複選框位置出現問題,我不知道如何使用提交按鈕提交這些已選中的預訂。在jsp中設置checkox的位置

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ page import ="java.sql.*" %> 




<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Book Ticket</title> 
</head> 
<body> 
<center>  
<table border="1" width="30%" height="30%"> 
    <tr><th><font color='#D18603'>Activity ID.</font></th> 
     <th><font color='#D18603'>Type</font></th> 
     <th><font color='#D18603'>Description</font></th> 
     <th><font color='#D18603'>City</font></th> 
     <th><font color='#D18603'>Location</font></th> 
     <th><font color='#D18603'>Date</font></th> 
     <th><font color='#D18603'>Price</font></th> 
     <th><font color='#D18603'>Buy</font></th> 
    </tr> 
    <td><b><font color='#663300'><input type="checkbox" name="ticket"  value=""/></font></td></b> 



    <% 
     Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance(); 
     Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123"); 

     Statement st = con.createStatement(); 
     ResultSet rs; 
     rs = st.executeQuery("select * from activities"); 
     while (rs.next()) { 

      String activityid1 = rs.getString("id"); 
      String type1 = rs.getString("type"); 
      String description1 = rs.getString("description"); 
      String city1 = rs.getString("city"); 
      String location1 = rs.getString("location"); 
      String date1 = rs.getString("date"); 
      String price1 = rs.getString("price"); 

      out.println("<tr>"); 
      out.println("<td>" + activityid1 + "</td>"); 
      out.println("<td>" + type1 + "</td>"); 
      out.println("<td>" + description1 + "</td>"); 
      out.println("<td>" + city1 + "</td>"); 
      out.println("<td>" + location1 + "</td>"); 
      out.println("<td>" + date1 + "</td>"); 
      out.println("<td>" + price1 + "</td>"); 

      out.println("</tr>"); 

     } 


    %> 

</center> 
</table></body> 
<tr> 
<td colspan="10"><input type="submit" value="Submit" /></td> 
<td><input type="reset" value="Reset" /></td> 
</tr> 
<br><br><a href='logout.jsp'>Log out</a> 
</html> 
+0

你是什麼意思的複選框位置? – PacMan

回答

0

請把複選框在while循環和複選框的值有activityid1。用表單圍繞代碼。創建java servlet或其他jsp。

<form method="post" action="<%=request.getContextPath()%>/TestServlet"> 
<table 
<tr> 
    ... 
while (rs.next()) { 

    int activityid1 = rs.getInt("id"); 
    String type1 = rs.getString("type"); 
       ... 

    out.println("<tr>"); 
    out.println("<td>" + activityid1 + "</td>"); 
    out.println("<td>" + type1 + "</td>"); 
    out.println("<td><b><font color='#663300'><input type='checkbox' name='ticket' value='"+activityid1+"'/></font></td></b>"); 

    out.println("</tr>"); 

} 
</table> 
    <td colspan="10"><input type="submit" value="Submit" /></td> 
</form> 

enter image description here

提交表單,您可以在另一個JSP得到遏制值,如test.jsp的或的TestServlet。

String [] val = request.getParameterValues("ticket"); 

最後,保存與USER_ID

你可以看看這個activityids。 MVC in JSP

也可以使用JSTL庫。不建議在JSP頁面中編寫更多的Java代碼。在Java類中連接數據庫。

我希望這會有所幫助。

+0

我不明白提交的部分 – tripley

+0

他會正確的代碼是什麼 – tripley