gpt4 book ai didi

java - 为什么随机复选框选择给出错误,而不是在 JSP 页面中按顺序排列?

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:44 26 4
gpt4 key购买 nike

我正在尝试制作一个基本的商店计费 JSP 项目。现在,当我使用复选框输入随机产品数量时,它会给出如下图所示的错误。但是当我用串行方式检查值时,它没有给出错误,它给了我我想要的结果。我还检查过它是否为空。但无法摆脱这个错误。我怎样才能摆脱它? enter image description here

enter image description here

代码:

<%-- 
Document : order
Created on : Jun 15, 2019, 3:11:06 PM
Author : Riddhi
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html lang="en">
<%@ include file="header.jsp" %>
<h2 class="text-center"> ShopBilling </h2>

<p><br/></p>
<%
String Host = "jdbc:mysql://localhost:3306/shopbilling";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
PreparedStatement ps=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(Host, "root", "");
StringBuilder sb = new StringBuilder();
%>
<div class="row justify-content-center">

<div class="col-md-6">
<div class="row">
<div class="col-md-6"><h3>Product Details</h3></div>
<div class="col-md-6 text-right">
<a href="products.jsp" class="btn btn-info">Back to Home</a>
</div>
</div>
<p></p>

<form action="" method="post">

<%
String products[] = request.getParameterValues("products");
String items[] = request.getParameterValues("item_no");

int sum=0;
if (products!= null && items!= null && products.length != 0 && items.length != 0) {
for (int i = 0; i < products.length; i++) {

%> <%
statement = connection.createStatement();
String u=request.getParameter("u");
int num=Integer.parseInt(products[i]);
String Data = "select * from products_tbl where id='"+num+"'";
rs = statement.executeQuery(Data);
while (rs.next()) {
%>
<input type="hidden" name="id" value='<%=rs.getString("id")%>'/>
<div class="form-group">
<label for="product_name">Product Name: <%=rs.getString("product_name")%></label>
<input type="hidden" class="form-control" id="product_name" name="product_name" value='<%=rs.getString("product_name")%>'>
</div>
<div class="form-group">
<label for="product_price">Product Single Price: <%=rs.getString("product_price")%></label>
<input type="hidden" class="form-control" id="product_price" name="product_price" value='<%=rs.getString("product_price")%>'>
</div>
<div class="form-group">
<label for="product_name">Item Quantity: <% out.println(items[i]); %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>

<div class="form-group">
<label for="product_name">Product Total Price: <%

int num1=Integer.parseInt(items[i]);
int propri=Integer.parseInt(rs.getString("product_price"));
out.println(num1 * propri); int gtotal= num1 * propri; %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>

<hr>


<% sum= sum + gtotal; %>


<%

}
}
}
%>

<div class="form-group">
<label for="product_name">Grand Total: <%
out.println(sum); %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>

<button type="print" onclick="window.print();" class="btn btn-warning">Print</button>


</form>
</div>
</div>
<%@ include file="footer.jsp" %>
</html>

最佳答案

我认为您的代码中的问题在于 <input type="text" name="item_no" />因为上面代码中的每一行都有 checkbox ,当您提交表单时仅选择checkbox已通过,但全部<input type="text" name="item_no" />甚至通过了 null你没有给出任何值,所以当你迭代时你会得到 null value现在,要解决这个问题,请执行以下操作:

//give value to your check-box i.e id of that row
<input type="checkbox" name="products" value ='<%=rs.getString("id")%>' />
//pass that id with your input type i.e item_no_1 ..etc
<input type="text" name='item_no_<%=rs.getString("id")%>' />

然后只获取 Product Quantity 的特定值您选择的位置 Checkbox如下所示:

 String products[] = request.getParameterValues("products"); 
if (products!= null && items!= null && products.length != 0 && items.length != 0) {
for (int i = 0; i < products.length; i++) {
//your code
<div class="form-group">
<label for="product_name">Product Total Price: <%
//getting item value of that selected checkbox
int item_no=Integer.parseInt(request.getParameter("item_no_" + products[i]));
int propri=Integer.parseInt(rs.getString("product_price"));
out.println(num1 * propri); int gtotal= num1 * propri; %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>

关于java - 为什么随机复选框选择给出错误,而不是在 JSP 页面中按顺序排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57033587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com