怎么样才能通过复选框从一个表格行中获取值(使用 JSP 和 HTML)?

huangapple go评论116阅读模式
英文:

How can I get value from one table row using checkbox ( JSP & HTML )?

问题

productCatalog.jsp

  1. <%@ page import="java.sql.*" %>
  2. <%@ page import="java.io.*" %>
  3. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Product Catalog</title>
  9. </head>
  10. <body>
  11. <%
  12. String sql;
  13. String output;
  14. String table;
  15. ResultSet rs;
  16. Class.forName("org.hsqldb.jdbc.JDBCDriver");
  17. Connection connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/", "SA", "");
  18. Statement stmt = connection.createStatement();
  19. rs = stmt.executeQuery("SELECT * FROM PRODUCT_CATALOG");
  20. output = "<tr><th>SELECT PRODUCT</th><th>PRODUCT ID</th><th>PRODUCT NAME</th><th>PRICE</th></tr>";
  21. while (rs.next()) {
  22. output += "<tr><td><input type='checkbox'></td>" +
  23. "<td>" + rs.getString(1) + "</td>" +
  24. "<td>" + rs.getString(2) + "</td>" +
  25. "<td>" + rs.getString(3) + "</td></tr>";
  26. }
  27. table = "<html>" +
  28. "<head>" +
  29. "</head>" +
  30. "<body>" +
  31. "<div>" +
  32. "<h1>Product Catalog</h1>" +
  33. "<form action='invoiceTable.jsp' method='POST'>" +
  34. "<table>" +
  35. output +
  36. "</table>" +
  37. "<button onclick='history.go(-1)' type='button'>Back</button>" +
  38. "<button type='submit'>Create invoice</button>" +
  39. "</form>" +
  40. "</div>" +
  41. "</body>" +
  42. "</html>";
  43. out.println(table);
  44. rs.close();
  45. %>
  46. </body>
  47. </html>

invoiceTable.jsp

  1. <%@ page import="java.sql.*" %>
  2. <%@ page import="java.io.*" %>
  3. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Invoices</title>
  9. </head>
  10. <body>
  11. <%
  12. String sql;
  13. String output;
  14. String table;
  15. ResultSet rs;
  16. String Articles = request.getParameter("???");
  17. String Total_price = request.getParameter("???");
  18. Class.forName("org.hsqldb.jdbc.JDBCDriver");
  19. Connection connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/", "SA", "");
  20. Statement stmt = connection.createStatement();
  21. sql = "INSERT INTO INVOICES (Articles, Total_price)" +
  22. "VALUES( '" + Articles + "', '" + Total_price + "');";
  23. stmt.executeUpdate(sql);
  24. rs = stmt.executeQuery("SELECT * FROM INVOICES");
  25. output = "<tr><th>INVOICE NR.</th><th>ARTICLES</th><th>TOTAL PRICE</th></tr>";
  26. while (rs.next()) {
  27. output += "<tr><td>" + rs.getString(1) + "</td>" +
  28. "<td>" + rs.getString(2) + "</td>" +
  29. "<td>" + rs.getString(3) + "</td></tr>";
  30. }
  31. table = "<html>" +
  32. "<head>" +
  33. "</head>" +
  34. "<body>" +
  35. "<div>" +
  36. "<h1>Invoices</h1>" +
  37. "<table>" +
  38. output +
  39. "</table>" +
  40. "</div>" +
  41. "</body>" +
  42. "</html>";
  43. out.println(table);
  44. rs.close();
  45. %>
  46. </body>
  47. </html>
英文:

So, I'm using JSP to display in internet page data from database. I have two tables ( "Product Catalog" & "Invoices" ). I want to choose one product from table ( Product Catalog ) with help of checkbox and insert into another table ( Invoices ).

怎么样才能通过复选框从一个表格行中获取值(使用 JSP 和 HTML)?

怎么样才能通过复选框从一个表格行中获取值(使用 JSP 和 HTML)?

productCatalog.jsp

  1. &lt;%@ page import=&quot;java.sql.*&quot; %&gt;
  2. &lt;%@ page import=&quot;java.io.*&quot; %&gt;
  3. &lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
  4. &lt;!DOCTYPE html&gt;
  5. &lt;html&gt;
  6. &lt;head&gt;
  7. &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
  8. &lt;title&gt;Product Catalog&lt;/title&gt;
  9. &lt;/head&gt;
  10. &lt;body&gt;
  11. &lt;%
  12. String sql;
  13. String output;
  14. String table;
  15. ResultSet rs;
  16. Class.forName(&quot;org.hsqldb.jdbc.JDBCDriver&quot;);
  17. Connection connection = DriverManager.getConnection(&quot;jdbc:hsqldb:hsql://localhost/&quot;, &quot;SA&quot;, &quot;&quot;);
  18. Statement stmt = connection.createStatement();
  19. rs = stmt.executeQuery(&quot;SELECT * FROM PRODUCT_CATALOG&quot;);
  20. output = &quot;&lt;tr&gt;&quot; + &quot;&lt;th&gt;SELECT PRODUCT&lt;/th&gt;&quot; + &quot;&lt;th&gt;PRODUCT ID&lt;/th&gt;&quot; + &quot;&lt;th&gt;PRODUCT NAME&lt;/th&gt;&quot; + &quot;&lt;th&gt;PRICE&lt;/th&gt;&lt;/tr&gt;&quot;;
  21. while (rs.next()) {
  22. output += &quot;&lt;tr&gt;&lt;td&gt;&lt;input type=&#39;checkbox&#39;&gt;&lt;/td&gt;&quot; + &quot;&lt;td&gt;&quot; + rs.getString(1) + &quot;&lt;/td&gt;&quot; + &quot;&lt;td&gt;&quot; + rs.getString(2) + &quot;&lt;/td&gt;&quot; + &quot;&lt;td&gt;&quot; + rs.getString(3) + &quot;&lt;/td&gt;&lt;/tr&gt;&quot;;
  23. }
  24. table = &quot;&lt;html&gt;&quot;
  25. + &quot;&lt;head&gt;&quot;
  26. + &quot;&lt;/head&gt;&quot;
  27. + &quot;&lt;body&gt;&quot;
  28. + &quot;&lt;div&gt;&quot;
  29. + &quot;&lt;h1&gt;Product Catalog&lt;/h1&gt;&quot;
  30. + &quot;&lt;form action=&#39;invoiceTable.jsp&#39; method=&#39;POST&#39;&gt;&quot;
  31. + &quot;&lt;table&gt;&quot;
  32. + output
  33. + &quot;&lt;/table&gt;&quot;
  34. + &quot;&lt;button onclick=&#39;history.go(-1)&#39; type=&#39;button&#39;&gt;Back&lt;/button&gt;&quot;
  35. + &quot;&lt;button type=&#39;submit&#39;&gt;Create invoice&lt;/button&gt;&quot;
  36. + &quot;&lt;/form&gt;&quot;
  37. + &quot;&lt;/div&gt;&quot;
  38. + &quot;&lt;/body&gt;&quot;
  39. + &quot;&lt;/html&gt;&quot;;
  40. out.println(table);
  41. rs.close();
  42. %&gt;
  43. &lt;/body&gt;
  44. &lt;/html&gt;

invoiceTable.jsp --- I wrote "???", because there must be data/values from table row which I selected. And I don't know which function or which parameter I must use.
String Articles = request.getParameter(&quot;???&quot;);

  1. &lt;%@ page import=&quot;java.sql.*&quot; %&gt;
  2. &lt;%@ page import=&quot;java.io.*&quot; %&gt;
  3. &lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
  4. &lt;!DOCTYPE html&gt;
  5. &lt;html&gt;
  6. &lt;head&gt;
  7. &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
  8. &lt;title&gt;Invoices&lt;/title&gt;
  9. &lt;/head&gt;
  10. &lt;body&gt;
  11. &lt;%
  12. String sql;
  13. String output;
  14. String table;
  15. ResultSet rs;
  16. String Articles = request.getParameter(&quot;???&quot;);
  17. String Total_price = request.getParameter(&quot;???&quot;);
  18. Class.forName(&quot;org.hsqldb.jdbc.JDBCDriver&quot;);
  19. Connection connection = DriverManager.getConnection(&quot;jdbc:hsqldb:hsql://localhost/&quot;, &quot;SA&quot;, &quot;&quot;);
  20. Statement stmt = connection.createStatement();
  21. sql = &quot;INSERT INTO INVOICES (Articles, Total_price)&quot;
  22. + &quot;VALUES( &#39;&quot; + Articles + &quot;&#39;, &#39;&quot; + Total_price + &quot;&#39;);&quot;;
  23. stmt.executeUpdate(sql);
  24. rs = stmt.executeQuery(&quot;SELECT * FROM INVOICES&quot;);
  25. output = &quot;&lt;tr&gt;&lt;th&gt;INVOICE NR.&lt;/th&gt;&quot; + &quot;&lt;th&gt;ARTICLES&lt;/th&gt;&quot; + &quot;&lt;th&gt;TOTAL PRICE&lt;/th&gt;&lt;/tr&gt;&quot;;
  26. while (rs.next()) {
  27. output += &quot;&lt;tr&gt;&lt;td&gt;&quot; + rs.getString(1) + &quot;&lt;/td&gt;&quot; + &quot;&lt;td&gt;&quot; + rs.getString(2) + &quot;&lt;/td&gt;&quot; + &quot;&lt;td&gt;&quot; + rs.getString(3) + &quot;&lt;/td&gt;&lt;/tr&gt;&quot;;
  28. }
  29. table = &quot;&lt;html&gt;&quot;
  30. + &quot;&lt;head&gt;&quot;
  31. + &quot;&lt;/head&gt;&quot;
  32. + &quot;&lt;body&gt;&quot;
  33. + &quot;&lt;div&gt;&quot;
  34. + &quot;&lt;h1&gt;Invoices&lt;/h1&gt;&quot;
  35. + &quot;&lt;table&gt;&quot;
  36. + output
  37. + &quot;&lt;/table&gt;&quot;
  38. + &quot;&lt;/form&gt;&quot;
  39. + &quot;&lt;/div&gt;&quot;
  40. + &quot;&lt;/body&gt;&quot;
  41. + &quot;&lt;/html&gt;&quot;;
  42. out.println(table);
  43. rs.close();
  44. %&gt;
  45. &lt;/body&gt;
  46. &lt;/html&gt;

答案1

得分: 1

首先,您需要在productCatalog.jsp中修改以下语句:

  1. while (rs.next()){
  2. output += "<tr><td><input type='checkbox'></td>" + "<td>" + rs.getString(1) + "</td>" + "<td>" + rs.getString(2) + "</td>" + "<td>" + rs.getString(3) + "</td></tr>";
  3. }

改为:

  1. int count=0;
  2. while (rs.next()){
  3. output+="<tr>";
  4. output+="<td><input type='checkbox' name='row"+(count++)+"' value="+rs.getString(1)+","+rs.getString(2)+","+rs.getString(3)+"></td>";
  5. output+="<td>" + rs.getString(1) + "</td>";
  6. output+="<td>" + rs.getString(2) + "</td>";
  7. output+="<td>" + rs.getString(3) + "</td></tr>";
  8. }
  9. output+="<input type=hidden name='rowCount' value='"+count+"'>";

在invoiceTable.jsp中,您可以参考以下代码提取所选行数据。

  1. String article="";
  2. String selectedRow;
  3. String[] temp;
  4. int totalPrice=0;
  5. int rowCount = Integer.parseInt(request.getParameter("rowCount"));
  6. if (rowCount>0){
  7. for (int i=0;i<rowCount;i++) {
  8. selectedRow=request.getParameter("row"+i);
  9. if (selectedRow!=null){ //that mean the row is selected by user.
  10. temp=selectedRow.split(","); //where temp[0]=product id,temp[1]=product name,temp[2]=product price.
  11. article+=temp[1]+" ";
  12. totalPrice+=Integer.parseInt(temp[2]);
  13. }
  14. }
  15. ....................... //prepare your SQL statement and insert the data to the database.
  16. }
英文:

First, you need to modify the following statement in productCatalog.jsp:

  1. while (rs.next()){
  2. output += &quot;&lt;tr&gt;&lt;td&gt;&lt;input type=&#39;checkbox&#39;&gt;&lt;/td&gt;&quot; + &quot;&lt;td&gt;&quot; + rs.getString(1) + &quot;&lt;/td&gt;&quot; + &quot;&lt;td&gt;&quot; + rs.getString(2) + &quot;&lt;/td&gt;&quot; + &quot;&lt;td&gt;&quot; + rs.getString(3) + &quot;&lt;/td&gt;&lt;/tr&gt;&quot;;
  3. }

to

  1. int count=0;
  2. while (rs.next()){
  3. output+=&quot;&lt;tr&gt;&quot;;
  4. output+=&quot;&lt;td&gt;&lt;input type=&#39;checkbox&#39; name=row&quot;+(count++)+&quot; value=&quot;+rs.getString(1)+&quot;,&quot;+rs.getString(2)+&quot;,&quot;+rs.getString(3)+&quot;&gt;&lt;/td&gt;&quot;;
  5. output+=&quot;&lt;td&gt;&quot; + rs.getString(1) + &quot;&lt;/td&gt;&quot;;
  6. output+=&quot;&lt;td&gt;&quot; + rs.getString(2) + &quot;&lt;/td&gt;&quot;;
  7. output+=&quot;&lt;td&gt;&quot; + rs.getString(3) + &quot;&lt;/td&gt;&lt;/tr&gt;&quot;;
  8. }
  9. output+=&quot;&lt;input type=hidden name=&#39;rowCount&#39; value=&#39;&quot;+count+&quot;&#39;&gt;&quot;;

In invoiceTable.jsp, you can refer the following coding to extract the selected row data.

  1. String article=&quot;&quot;;
  2. String selectedRow;
  3. String[] temp;
  4. int totalPrice=0;
  5. int rowCount = Integer.parse(request.getParameter(&quot;rowCount&quot;));
  6. if (rowCount&gt;0){
  7. for (int i=0;i&lt;rowCount;i++) {
  8. selectedRow=request.getParameter(&quot;row&quot;+i);
  9. if (selectedRow!=null){ //that mean the row is selected by user.
  10. temp=selectedRow.split(&quot;,&quot;); //where temp[0]=product id,temp[1]=product name,temp[2]=product price.
  11. article+=temp[1]+&quot; &quot;;
  12. totalPrice+=Integer.parse(temp[2]);
  13. }
  14. }
  15. ....................... //prepare your SQL statement and insert the data to the database.
  16. }

huangapple
  • 本文由 发表于 2020年10月22日 23:00:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64485033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定