Java 如何使用复选框(JSP和HTML)从一个表行中获取值?
所以,我使用JSP在internet页面中显示数据库中的数据。我有两张表(“产品目录”和“发票”)。我想借助于复选框和插入另一个表(发票),从表(产品目录)中选择一种产品 productCatalog.jspJava 如何使用复选框(JSP和HTML)从一个表行中获取值?,java,html,jsp,Java,Html,Jsp,所以,我使用JSP在internet页面中显示数据库中的数据。我有两张表(“产品目录”和“发票”)。我想借助于复选框和插入另一个表(发票),从表(产品目录)中选择一种产品 productCatalog.jsp <%@ page import="java.sql.*" %> <%@ page import="java.io.*" %> <%@page contentType="text/html" pa
<%@ 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>
首先,您需要修改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>";
}
while(rs.next()){
输出+=++++++rs.getString(1)+++++++rs.getString(2)+++++++rs.getString(3)+”;
}
到
int count=0;
while(rs.next()){
输出+=“”;
输出+=“”;
输出+=“”+rs.getString(1)+“”;
输出+=“”+rs.getString(2)+“”;
输出+=“”+rs.getString(3)+“”;
}
输出+=“”;
在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.
}
String article=”“;
字符串selectedRow;
字符串[]温度;
int totalPrice=0;
int rowCount=Integer.parse(request.getParameter(“rowCount”);
如果(行计数>0){
对于(int i=0;此处代码中的iTyposI@TheKNVB谢谢,它工作得很好,但是,也许你们知道我如何从所选产品中求和价格?因为,现在,当我选择两个或更多产品并插入到发票表时,它们被分别添加为发票id 1和2,所以我希望它们被添加到一个订单中,例如(发票编号:1,产品:冰茶热茶,总价:21)。如果您知道我将非常感谢您!@KNVB它完全按照我的要求工作!!!谢谢!!!
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+"'>";
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.
}