英文:
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 ).
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  --- 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("???");
<%@ 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>
答案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 += "<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.
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.          
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论