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

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

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

问题

productCatalog.jsp

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Product Catalog</title>
    </head>
    <body>
        <%
            String sql;
            String output;
            String table;
            ResultSet rs;
            Class.forName("org.hsqldb.jdbc.JDBCDriver");
            Connection connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/", "SA", "");

            Statement stmt = connection.createStatement();
            rs = stmt.executeQuery("SELECT * FROM PRODUCT_CATALOG");

            output = "<tr><th>SELECT PRODUCT</th><th>PRODUCT ID</th><th>PRODUCT NAME</th><th>PRICE</th></tr>";
            while (rs.next()) {
                output += "<tr><td><input type='checkbox'></td>" +
                          "<td>" + rs.getString(1) + "</td>" +
                          "<td>" + rs.getString(2) + "</td>" +
                          "<td>" + rs.getString(3) + "</td></tr>";
            }
            table = "<html>" +
                    "<head>" +
                    "</head>" +
                    "<body>" +
                    "<div>" +
                    "<h1>Product Catalog</h1>" +
                    "<form action='invoiceTable.jsp' method='POST'>" +
                    "<table>" +
                    output +
                    "</table>" +
                    "<button onclick='history.go(-1)' type='button'>Back</button>" +
                    "<button type='submit'>Create invoice</button>" +
                    "</form>" +
                    "</div>" +
                    "</body>" +
                    "</html>";
            out.println(table);
            rs.close();
        %>
    </body>
</html>

invoiceTable.jsp

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Invoices</title>
    </head>
    <body>
        <%
            String sql;
            String output;
            String table;
            ResultSet rs;
            String Articles = request.getParameter("???");
            String Total_price = request.getParameter("???");
            Class.forName("org.hsqldb.jdbc.JDBCDriver");
            Connection connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/", "SA", "");

            Statement stmt = connection.createStatement();

            sql = "INSERT INTO INVOICES (Articles, Total_price)" +
                  "VALUES( '" + Articles + "', '" + Total_price + "');";
            stmt.executeUpdate(sql);

            rs = stmt.executeQuery("SELECT * FROM INVOICES");

            output = "<tr><th>INVOICE NR.</th><th>ARTICLES</th><th>TOTAL PRICE</th></tr>";
            while (rs.next()) {
                output += "<tr><td>" + rs.getString(1) + "</td>" +
                          "<td>" + rs.getString(2) + "</td>" +
                          "<td>" + rs.getString(3) + "</td></tr>";
            }
            table = "<html>" +
                    "<head>" +
                    "</head>" +
                    "<body>" +
                    "<div>" +
                    "<h1>Invoices</h1>" +
                    "<table>" +
                    output +
                    "</table>" +
                    "</div>" +
                    "</body>" +
                    "</html>";
            out.println(table);

            rs.close();
        %>
    </body>
</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

&lt;%@ page import=&quot;java.sql.*&quot; %&gt; 
&lt;%@ page import=&quot;java.io.*&quot; %&gt;
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;Product Catalog&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;

        &lt;%

            String sql;
            String output;
            String table;
            ResultSet rs;
            Class.forName(&quot;org.hsqldb.jdbc.JDBCDriver&quot;);
            Connection connection = DriverManager.getConnection(&quot;jdbc:hsqldb:hsql://localhost/&quot;, &quot;SA&quot;, &quot;&quot;);

            Statement stmt = connection.createStatement();
            rs = stmt.executeQuery(&quot;SELECT * FROM PRODUCT_CATALOG&quot;);

            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;;
            while (rs.next()) {
                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;;
            }
            table = &quot;&lt;html&gt;&quot;
                    + &quot;&lt;head&gt;&quot;
                    + &quot;&lt;/head&gt;&quot;
                    + &quot;&lt;body&gt;&quot;
                    + &quot;&lt;div&gt;&quot;
                    + &quot;&lt;h1&gt;Product Catalog&lt;/h1&gt;&quot;
                    + &quot;&lt;form action=&#39;invoiceTable.jsp&#39; method=&#39;POST&#39;&gt;&quot;
                    + &quot;&lt;table&gt;&quot;
                    + output
                    + &quot;&lt;/table&gt;&quot;
                    + &quot;&lt;button onclick=&#39;history.go(-1)&#39; type=&#39;button&#39;&gt;Back&lt;/button&gt;&quot;
                    + &quot;&lt;button type=&#39;submit&#39;&gt;Create invoice&lt;/button&gt;&quot;
                    + &quot;&lt;/form&gt;&quot;
                    + &quot;&lt;/div&gt;&quot;
                    + &quot;&lt;/body&gt;&quot;
                    + &quot;&lt;/html&gt;&quot;;
            out.println(table);
            rs.close();
        %&gt;
    &lt;/body&gt;
&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;);

&lt;%@ page import=&quot;java.sql.*&quot; %&gt; 
&lt;%@ page import=&quot;java.io.*&quot; %&gt;
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;Invoices&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;

        &lt;%

            String sql;
            String output;
            String table;
            ResultSet rs;
            String Articles = request.getParameter(&quot;???&quot;);
            String Total_price = request.getParameter(&quot;???&quot;);
            Class.forName(&quot;org.hsqldb.jdbc.JDBCDriver&quot;);
            Connection connection = DriverManager.getConnection(&quot;jdbc:hsqldb:hsql://localhost/&quot;, &quot;SA&quot;, &quot;&quot;);

            Statement stmt = connection.createStatement();

            sql = &quot;INSERT INTO INVOICES (Articles, Total_price)&quot;
                    + &quot;VALUES( &#39;&quot; + Articles + &quot;&#39;, &#39;&quot; + Total_price + &quot;&#39;);&quot;;
            stmt.executeUpdate(sql);

            rs = stmt.executeQuery(&quot;SELECT * FROM INVOICES&quot;);

            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;;
            while (rs.next()) {
                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;;
            }
            table = &quot;&lt;html&gt;&quot;
                    + &quot;&lt;head&gt;&quot;
                    + &quot;&lt;/head&gt;&quot;
                    + &quot;&lt;body&gt;&quot;
                    + &quot;&lt;div&gt;&quot;
                    + &quot;&lt;h1&gt;Invoices&lt;/h1&gt;&quot;
                    + &quot;&lt;table&gt;&quot;
                    + output
                    + &quot;&lt;/table&gt;&quot;
                    + &quot;&lt;/form&gt;&quot;
                    + &quot;&lt;/div&gt;&quot;
                    + &quot;&lt;/body&gt;&quot;
                    + &quot;&lt;/html&gt;&quot;;
            out.println(table);

            rs.close();
        %&gt;
    &lt;/body&gt;
&lt;/html&gt;

答案1

得分: 1

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

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

改为:

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

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

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

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

while (rs.next()){
	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;;
}

to

int count=0;
while (rs.next()){
	output+=&quot;&lt;tr&gt;&quot;;
	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;;
	output+=&quot;&lt;td&gt;&quot; + rs.getString(1) + &quot;&lt;/td&gt;&quot;; 
	output+=&quot;&lt;td&gt;&quot; + rs.getString(2) + &quot;&lt;/td&gt;&quot;;
	output+=&quot;&lt;td&gt;&quot; + rs.getString(3) + &quot;&lt;/td&gt;&lt;/tr&gt;&quot;;
}
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.

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

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:

确定