简体   繁体   English

如何使用复选框(JSP 和 HTML)从表格行中获取值?

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

So, I'm using JSP to display in internet page data from database.所以,我使用 JSP 来显示数据库中的网页数据。 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 ).我想在复选框的帮助下从表(产品目录)中选择一个产品并插入另一个表(发票)。

在此处输入图片说明

在此处输入图片说明

productCatalog.jsp产品目录.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 --- I wrote "???", because there must be data/values from table row which I selected. invoiceTable.jsp --- 我写了“???”,因为我选择的表行中必须有数据/值。 And I don't know which function or which parameter I must use.而且我不知道我必须使用哪个函数或哪个参数。 String Articles = request.getParameter("???");

<%@ 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>"
                    + "</form>"
                    + "</div>"
                    + "</body>"
                    + "</html>";
            out.println(table);

            rs.close();
        %>
    </body>
</html>

First, you need to modify the following statement in productCatalog.jsp:首先需要修改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>";
}

to

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+"'>";    

In invoiceTable.jsp, you can refer the following coding to extract the selected row data.在invoiceTable.jsp 中,您可以参考以下代码提取选中的行数据。

String article="";
String selectedRow;
String[] temp;
int totalPrice=0;
int rowCount = Integer.parse(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.parse(temp[2]); 
        }
    }
    .......................  //prepare your SQL statement and insert the data to the database.          
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM