【问题标题】:How can I get value from one table row using checkbox ( JSP & HTML )?如何使用复选框( JSP 和 HTML )从一个表行中获取值?
【发布时间】:2020-10-23 09:11:50
【问题描述】:

所以,我使用 JSP 来显示来自数据库的互联网页面数据。我有两个表(“产品目录”和“发票”)。我想在 checkboxinsert into 另一个表(Invoices)的帮助下从表(产品目录)中选择一个产品。

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 --- 我写了“???”,因为我选择的表行中必须有数据/值。而且我不知道我必须使用哪个函数或哪个参数。 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>

【问题讨论】:

    标签: java html jsp


    【解决方案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.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.          
    }
    

    【讨论】:

    • 代码中的错别字I&lt;rowCount,Interger,articel.
    • @The KNVB 谢谢,它工作得很好,但是,也许,你知道我怎样才能对选定产品的价格求和吗?因为,现在,当我选择两个或多个产品并插入发票表时,它们分别作为发票 id 1 和 2 添加,所以我希望将它们添加到一个订单中,例如(发票 id:1,产品:冰茶热茶,总价:21)。如果你知道我会非常感谢你!
    • @The KNVB 它完全符合我的要求!!!谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2023-03-19
    • 1970-01-01
    • 2011-11-22
    相关资源
    最近更新 更多